简体   繁体   English

调用 mysqli_multi_query 后我的 php 代码无法运行的原因可能是什么?

[英]What may be the cause to my php code does not run after mysqli_multi_query is called?

I simply tried using "mysqli_multi_query" function in my php code (Queries are separated by semi-colon) and it inserts data correctly.我只是尝试在我的 php 代码中使用“mysqli_multi_query”function(查询由分号分隔)并正确插入数据。

I'm not getting any error after "mysqli_multi_query" is called.调用“mysqli_multi_query”后我没有收到任何错误。 But the php code below the "mysqli_multi_query" statement (simple echo to test) are not reached.但未达到“mysqli_multi_query”语句(简单回显测试)下方的 php 代码。

When I replace "mysqli_multi_query" with "mysqli_query" inside a loop, total php code works without problem.当我在循环中用“mysqli_query”替换“mysqli_multi_query”时,总的 php 代码可以正常工作。

Is this a known behaviour or is there something else I need to know about "mysqli_multi_query"?这是一种已知的行为,还是我需要了解有关“mysqli_multi_query”的其他信息? Is mysqli executes multiple queries asynchronously? mysqli 是否异步执行多个查询?

I'm sorry my code is at the home pc and unable to post it here until next week end.很抱歉,我的代码在家用电脑上,直到下周末才能在这里发布。 I checked all the queries by manually running on the workbench and they works perfectly.我通过在工作台上手动运行检查了所有查询,它们运行良好。

I still can fix this by running each query separately in a for loop, but hope to reduce the number of database hits and increase the performance with "mysqli_multi_query".我仍然可以通过在 for 循环中单独运行每个查询来解决此问题,但希望减少数据库命中的数量并使用“mysqli_multi_query”提高性能。

I saw some similar posts here but none of them properly addressed my question.我在这里看到了一些类似的帖子,但没有一个能正确解决我的问题。 Any support would be highly appreciated.任何支持将不胜感激。

The code was something like this..代码是这样的..

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql1 = "insert into test (testCol) values(1);";
$sql2 = "insert into test (testCol) values(2);";

mysqli_multi_query($con, $sql1.$sql2 ); /* data inserted properly in test table. No error. */

/* Any code below here is not reached. But when I used below commented queries instead of above (mysqli_multi_query), echo is called.

    mysqli_query($con, $sql1); 
    mysqli_query($con, $sql2); 

 */

echo "Success"; 

mysqli_close($con);
?>

Yes it's mostly something what you guess.是的,这主要是您的猜测。 if you mix mysqli_multi_query and mysqli_query, the latter(s) won't be executed!如果混合使用 mysqli_multi_query 和 mysqli_query,后者将不会被执行!

problamatic code:问题代码:

$mysqli->multi_query(" Many SQL queries ; "); //works
$mysqli->query(" SQL statement #1 ; ") // not works!

The only way to do this correctly is:正确执行此操作的唯一方法是:

$mysqli->multi_query(" Many SQL queries ; "); // works
while ($mysqli->next_result()) {;} // flush multi_queries
$mysqli->query(" SQL statement #1 ; ") // works!

You just need to use/flush results to make the rest of php code to work.您只需要使用/刷新结果即可使 php 代码的 rest 工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM