有多个准备好的语句的问题 [英] Problem with multiple prepared statements

查看:54
本文介绍了有多个准备好的语句的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问题.我有一个准备好的声明,像这样:

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
");

并在以后执行它们:

$select_something->execute();
$select_something->bind_result($whatever);

第一个语句被执行,第二个语句在以上两行均引发此错误:

the first statement gets executed, and the second one throws this error for both lines above:

警告:mysqli_stmt :: execute()[mysqli-stmt.execute]:无效的对象或资源mysqli_stmt

Warning: mysqli_stmt::execute() [mysqli-stmt.execute]: invalid object or resource mysqli_stmt

请注意,语句的命名不同($ select_something和$ select_something_else),我只是认为无需重复代码.

Note, statements are named differently ($select_something and $select_something_else), I just thought it's needless to repeat the code.

推荐答案

请注意,mysqli扩展已由 PDO替换,它更易于使用且表现更好.您应该使用它而不是mysqli.如果您需要PDO教程,请尝试"使用PHP和PDO编写MySQL脚本" .即使您进行切换,您仍可能希望找出问题所在,在这种情况下,请继续阅读.

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.

该特定错误仅表示您不在处理$select_something$select_something_else中的mysqli_stmt.许多扩展(尤其是DB扩展)中的函数在失败时均返回false,而不是资源或对象.这可能就是这里发生的事情.通过测试可能失败的函数的返回值,遵循正确的错误处理过程.使用 mysqli::error 获取有关MySQLi函数/方法失败的原因的描述.

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.

您可能会遇到mysqli和MySQL驱动程序的局限性:较低级别 mysqli::query() ).正确应用 mysqli_stmt::store_result 可能会解决此问题.

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.

请注意,您不需要调用mysqli::stmt_init(),它已经存在(作为mysqli_stmt_init)来支持过程编程.相反,您可以使用 mysqli::prepare() .

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().

这篇关于有多个准备好的语句的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆