在我的 php 代码中,我使用了一个 while 循环,如下所示。
<?php
$connection=mysqli_connect("localhost","root","","entries");
$query1="select * from jobs";
$query1exe=mysqli_query($connection,$query1);
$loopcount=0;
while($query1collector=mysqli_fetch_array($query1exe,MYSQLI_ASSOC)) {
$query2="insert into learning set name='new_1';
insert into learning set name='new_2';";
mysqli_multi_query($connection,$query2);
$loopcount++;
}
echo($loopcount);
?>
在这段代码中,我得到的输出为 104($loopcount),这意味着循环执行了 104 次。但是在名为 learning 的表中,我只有两个新条目 new_1 和 new_2。我预计有 208 个新条目(new_1 104 次和 new_2 104 次)。为什么我没有得到预期的结果。
我使用的是 PHP 版本 5.5.11
P.S 我想使用 mysqli_multi_query 本身,因为我需要同时执行几个查询。
这是我执行代码 3 次后我的学习表的样子
--------------------------------------------------------
slno | name | city | Phone
--------------------------------------------------------
1 | sandeep | NULL | NULL
-------------------------------------------------------
2 | new_1 | NULL | NULL
-------------------------------------------------------
3 | new_2 | NULL | NULL
-------------------------------------------------------
4 | new_1 | NULL | NULL
-------------------------------------------------------
5 | new_2 | NULL | NULL
------------------------------------------------------
6 | new_1 | NULL | NULL
------------------------------------------------------
7 | new_2 | NULL | NULL
-------------------------------------------------------
最佳答案
我看了官方文档here并找到了一条用户评论
if you mix $mysqli->multi_query and $mysqli->query, the latter(s) won't be executed!
仅使用 mysqli_query
,将您的多查询拆分为:
mysqli_query($connection,"insert into learning set name='new_1'");
mysqli_query($connection,"insert into learning set name='new_2'");
关于php - mysqli_multi_query 没有在 while 循环中多次执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33286946/