将运算符作为参数传递给 odbc_execute()

发布于 2024-12-06 08:45:15 字数 626 浏览 0 评论 0原文

我正在尝试向准备好的陈述迈出第一步(结果一败涂地)。

以前,我从 $_GET 构建了以下内容并回显它 - 代码运行良好,并且它返回了我从简单测试数据库中期望的内容。

SELECT * FROM edit_box WHERE (tag="9") AND (text="mango") ORDER BY time_stamp DESC

当我尝试使用准备好的语句对其进行编码时,即使我不使用 $_GET 而只是对之前的值进行硬编码,我的代码看起来就像这样,

$odbc_query = OdbcPrepare('SELECT * FROM edit_box WHERE (tag="?")' .
                          ' AND (text ? "?") ORDER BY time_stamp DESC');
           
$odbcResult = odbc_exec($odbc_query, array('9',  '=', 'mango'));  
var_dump($odbcResult);

我得到了 NULL

显然这是一个初学者的错误,但我盯着它看,仍然没有说“d'oh!”

我做错了什么?

I am taking my first tentative steps into prepared statements (and falling flat on my face).

Previously, I built the following from $_GET and echoed it back - the code was working fine and it returned what I expected from my simple test database.

SELECT * FROM edit_box WHERE (tag="9") AND (text="mango") ORDER BY time_stamp DESC

and when I try to code it using a prepared statement, even if I don't use $_GET but just hard-code the values from the previous, my code looks like this

$odbc_query = OdbcPrepare('SELECT * FROM edit_box WHERE (tag="?")' .
                          ' AND (text ? "?") ORDER BY time_stamp DESC');
           
$odbcResult = odbc_exec($odbc_query, array('9',  '=', 'mango'));  
var_dump($odbcResult);

I get NULL.

Obviously a beginner mistake, but I stare at it and still don't say d'oh!

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人接听 2024-12-13 08:45:15

你不能这样做 -

AND (text ? "?")

像这样的参数通常只能传递实际值 - 在某些情况下是标识符......

要做你想做的事,你需要插入 '=' 内联进入 SQL 语句...

有点像这样 --

$logical_operator = '=';

$sql = SELECT * FROM edit_box WHERE (tag=\"?\") AND (text $logical_operator \"?\") ORDER BY time_stamp DESC');

$odbc_query = OdbcPrepare($sql);

$odbcResult = odbc_exec($odbc_query, array('9', 'mango'));  

You cannot do this --

AND (text ? "?")

Parameters, like this, can usually only be passed for actual values - and in some cases identifiers...

To do what you want you need to interpolate the '=' inline into the SQL statement...

Kind of, like this --

$logical_operator = '=';

$sql = SELECT * FROM edit_box WHERE (tag=\"?\") AND (text $logical_operator \"?\") ORDER BY time_stamp DESC');

$odbc_query = OdbcPrepare($sql);

$odbcResult = odbc_exec($odbc_query, array('9', 'mango'));  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文