微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – mysqli_store_result()实际上做了什么?

我想明白 mysqli_store_result究竟做了什么?当我访问了MysqLi_store_result的 PHP Manual时,我找到了定义
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.”

资料来源:PHP: mysqli_store_result

当我执行上面提到的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_store_result()不能与INSERT语句一起使用,但没有人提到实际的适当函数MysqLi_affected_rows().
如果您的语句返回一个记录集并且您想以数字方式检查它,那么使用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()似乎是发送一批查询的一种很酷,简洁的方法,但是使用此功能并没有太多令人信服的理由/场景,而不是以安全的方式一次一个地发送查询.

原文地址:https://www.jb51.cc/php/134554.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐