多个准备好的语句的问题

发布于 2024-10-18 03:57:08 字数 768 浏览 2 评论 0原文

问题就在这里。我有一个准备好的声明,如下所示:

$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 技术交流群。

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

发布评论

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

评论(1

听闻余生 2024-10-25 03:57:08

请注意,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) return false 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. Use mysqli::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() or mysql_store_result() must be called on results before they can be used. By default, mysqli_stmt::execute probably calls mysql_use_result, which means the old result must be closed before a new query is run (which is mentioned in the documentation for mysqli::query()). Proper application of mysqli_stmt::store_result may fix the problem.

Note that you shouldn't need to call mysqli::stmt_init(), which is present (as mysqli_stmt_init) to support procedural programming. Instead, you can use mysqli::prepare().

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