带有准备好的语句的多查询
我试图了解 多查询 在 mysqli 中如何工作。但我承认这并不容易理解。
基本上我如何在多重查询中执行这些查询?该页面没有讨论多查询中的准备语句。
($sql = $db -> prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)"));
$sql -> bind_param('sss', $name, $email, $hash);
$sql -> execute();
($sq = $db -> prepare("INSERT INTO activation_link_password_reset (activation_link) VALUES (?)"));
$sq -> bind_param('s', $linkHash);
$sq -> execute();
I am trying understand how multi queries work in mysqli. But I confess that is not easy to understand.
Basically how I can do these queries in a multi query? The page doesn't talk about prepared statements in multi queries.
($sql = $db -> prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)"));
$sql -> bind_param('sss', $name, $email, $hash);
$sql -> execute();
($sq = $db -> prepare("INSERT INTO activation_link_password_reset (activation_link) VALUES (?)"));
$sq -> bind_param('s', $linkHash);
$sq -> execute();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在那里使用准备好的语句,并且加速也可以忽略不计,因此请选择更容易调试单独的查询。
如果您确实想使用准备好的语句在 1 次调用中完成此操作,请创建一个
PROCEDURE
(调试起来更加困难...),并准备CALL(:param1,:param2);
。You can't use prepared statements there, and the speedup is also negligible, so go for the easier to debug seperate queries.
If you really want to do it in 1 call with prepared statements, create a
PROCEDURE
(even more difficult to debug...), and prepareCALL(:param1,:param2);
.事实上我几天前才明白这一点。迁移到 MySQLi 并不是一件小事,但我将继续向您展示准备好的语句的查询循环。
这里的所有内容都与非 oo 格式类似,通过替换 ->;使用 _ 并将返回值传递到新函数中,但这里的过程几乎就是您应该对每个语句执行的操作。您也可以使用 mysqli 包装类将其抽象出来。
在
$db->next_result()
调用之后,您可以再次重复该过程。此更改与 MySQLi 处理 MySQL 连接和结果的方式有关。当然,您可以通过切换回 mysql_ 查询和资源来完全避免此问题。特别是对于 multi_query,您可以在单个字符串中用分号分隔查询。您链接到的示例显示了如何处理结果。
MySQLi 书籍@PHP.net
I actually just figured this out a few days ago. The move to MySQLi is not a trivial one but ill go ahead and show you the query loop for prepared statements.
Everything here works similar to the non oo format by replacing -> with _ and passing the returned value into the new function, but this process here is pretty much what you should look to be doing with each of those statements. You can abstract it away with a mysqli wrapper class as well.
After the
$db->next_result()
call you are able to repeat the process again. This change has to do with the way the MySQL connection and results are handled with MySQLi. You can, of course, avoid this issue entirely by switching back to mysql_ queries and resources.Specifically for multi_query, you separate your queries with a semicolon, in a single string. The example you linked to shows how to process the results.
MySQLi Book @PHP.net