MysqLi_store_result — Transfers a result set from the last query
问题是它转移结果集的位置?
实际上我在执行MysqLi_multi_query后得到错误“命令不同步;你现在无法运行此命令”但是当我使用以下方法时,错误就消失了.
MysqLi_multi_query($connection,$query); do { MysqLi_store_result($connection); } while(MysqLi_next_result($connection));
现在,我应该在每个MysqLi_query之后或者在MysqLi_multi_query之后使用这个MysqLi_store_result($connection)和MysqLi_next_result($connection)因为我已经在PHP Manaul中读过
“Although it is always good practice to free the memory used by the
result of a query using the MysqLi_free_result() function,when
transferring large result sets using the MysqLi_store_result() this
becomes particularly important.”
当我执行上面提到的MysqLi_multi_query($connection,$query)时,还有一个问题出现了;我把一个声明echo’存储结果< br />‘如下
do { echo 'storing result <br /> MysqLi_store_result($connection); } while(MysqLi_next_result($connection));
虽然$query中只有两个INSERT查询,但它给出了以下输出
storing result storing result storing result storing result
这意味着有四个结果集被转移.我无法理解这种情况.
最后一个问题.上述过程是否会影响性能?
如果您的语句返回一个记录集并且您想以数字方式检查它,那么使用MysqLi_num_rows().
如果处理混合物,这可能会让你开始:
$queries[] = "INSERT INTO TestTable (Column1) VALUES ('TEST1')"; $queries[] = "SELECT * FROM TestTable WHERE Column1 LIKE 'TEST%'"; $queries[] = "INSERT INTO TestTable (Column1) VALUES ('TEST2')"; $queries[] = "SELECT * FROM TestTable WHERE Column1 LIKE 'TEST%'"; $queries[] = "DELETE FROM TestTable WHERE Column1 LIKE 'TEST%'"; if(MysqLi_multi_query($con,implode(';',$queries))){ do{ if($result = MysqLi_store_result($con)){ echo "Selected rows = " . MysqLi_num_rows($result) . "<br><br>"; MysqLi_free_result($result); }else{ $cumulative_rows += $aff_rows = MysqLi_affected_rows($con); echo "Current Query's Affected Rows = $aff_rows,Cumulative Rows = $cumulative_rows<br><br>"; } } while(MysqLi_more_results($con) && MysqLi_next_result($con)); }
输出:
Current Query's Affected Rows = 1,Cumulative Affected Rows = 1 Selected rows = 1 Current Query's Affected Rows = 1,Cumulative Affected Rows = 2 Selected rows = 2 Current Query's Affected Rows = 2,Cumulative Affected Rows = 4
对于数据库查询主题的新手来说,这是一个重要的注意事项:如果您使用的是用户提供的/外部源/不可信的数据,那么您应该使用带有占位符的预准备语句来实现安全性/稳定性(MysqLi_multi_query()不支持此事) .使用MysqLi_multi_query()似乎是发送一批查询的一种很酷,简洁的方法,但是使用此功能并没有太多令人信服的理由/场景,而不是以安全的方式一次一个地发送查询.