多个准备好的语句的问题
问题就在这里。我有一个准备好的声明,如下所示:
$select_something = $db->stmt_init();
$select_something->prepare ("
SELECT whatever
FROM table
");
$select_something->execute();
$select_something->bind_result($whatever);
独自一人时 - 它有效。当我在执行后添加另一个时,它也可以工作。 但是,当我尝试首先准备它们:
$select_something = $db->stmt_init();
$select_something->prepare ("
SELECT whatever
FROM table
");
并稍后执行它们时:
$select_something->execute();
$select_something->bind_result($whatever);
第一个语句被执行,第二个语句对上面的两行抛出此错误:
警告:mysqli_stmt::execute() [mysqli-stmt.execute]:无效对象或资源 mysqli_stmt
注意,语句的命名不同($select_something 和 $select_something_else),我只是认为无需重复代码。
Here's the issue. I have a prepared statement, like this:
$select_something = $db->stmt_init();
$select_something->prepare ("
SELECT whatever
FROM table
");
$select_something->execute();
$select_something->bind_result($whatever);
When alone - it works. When I add another one after it's execution it also works.
But when I try to just first prepare them both:
$select_something = $db->stmt_init();
$select_something->prepare ("
SELECT whatever
FROM table
");
and execute them later on:
$select_something->execute();
$select_something->bind_result($whatever);
the first statement gets executed, and the second one throws this error for both lines above:
Warning: mysqli_stmt::execute() [mysqli-stmt.execute]: invalid object or resource mysqli_stmt
Note, statements are named differently ($select_something and $select_something_else), I just thought it's needless to repeat the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,mysqli 扩展已被 PDO 取代,它更易于使用且性能更好。你应该使用它而不是 mysqli。如果您需要 PDO 教程,请尝试“使用 PHP 和 PDO 编写 MySQL 脚本” 。即使您切换,您可能仍然希望找出问题所在,在这种情况下请继续阅读。
该特定错误仅意味着您没有在
$select_something
或$select_something_else
中处理mysqli_stmt
。许多扩展(尤其是数据库扩展)中的函数在失败时返回 false,而不是资源或对象。这可能就是这里发生的事情。通过测试可能失败的函数的返回值来遵循正确的错误处理程序。使用mysqli::error
获取 MySQLi 函数/方法原因的描述正在失败。您可能会遇到 mysqli 和 MySQL 驱动程序的限制:较低级别
mysql_use_result()
或mysql_store_result()
必须在结果上调用才能使用。默认情况下,mysqli_stmt::execute
可能调用mysql_use_result
,这意味着旧结果必须是在运行新查询之前关闭(mysqli::query()
)。正确应用mysqli_stmt::store_result
可能会解决该问题。请注意,您不需要调用
mysqli::stmt_init()
,它的存在(作为mysqli_stmt_init
)是为了支持过程式编程。相反,您可以使用mysqli::prepare()
。Note that the mysqli extension has been replaced by PDO, which is simpler to use and better behaved. You should use it rather than mysqli. If you need a PDO tutorial, try "Writing MySQL Scripts with PHP and PDO". Even if you switch, you may still wish to figure out what is going wrong, in which case read on.
That particular error simply means you're not dealing with a
mysqli_stmt
in$select_something
or$select_something_else
. Functions in many extensions (especially DB extensions) returnfalse
when they fail, rather than a resource or object. That's probably what's happening here. Follow proper error handling procedure by testing the return value of functions that could fail. Usemysqli::error
to get a description of why a MySQLi function/method is failing.You may be running across a limitation of mysqli and the MySQL driver: the lower level
mysql_use_result()
ormysql_store_result()
must be called on results before they can be used. By default,mysqli_stmt::execute
probably callsmysql_use_result
, which means the old result must be closed before a new query is run (which is mentioned in the documentation formysqli::query()
). Proper application ofmysqli_stmt::store_result
may fix the problem.Note that you shouldn't need to call
mysqli::stmt_init()
, which is present (asmysqli_stmt_init
) to support procedural programming. Instead, you can usemysqli::prepare()
.