将运算符作为参数传递给 odbc_execute()
我正在尝试向准备好的陈述迈出第一步(结果一败涂地)。
以前,我从 $_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能这样做 -
AND (text ? "?")
像这样的参数通常只能传递实际值 - 在某些情况下是标识符......
要做你想做的事,你需要插入 '=' 内联进入 SQL 语句...
有点像这样 --
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 --