PHP :::预备语句::: freeresult()::: close() [英] PHP ::: Prepared Statements ::: freeresult() ::: close()

查看:95
本文介绍了PHP :::预备语句::: freeresult()::: close()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的重要性是什么

$stmt->free_result();
$stmt->close();

使用如下准备好的语句进行数据库调用后:

After a database call using prepared statments like this:

$mysqli=new mysqli("database", "db", "pass", "user");

$stmt = $mysqli->prepare("SELECT email FROM users WHERE id=? ");
$stmt->bind_param('i',$_SESSION['id']);
$stmt->execute();
$stmt->bind_result($email);
while($stmt->fetch()){
     echo $email;
}
$stmt->free_result(); //why do i need this?
$stmt->close();       //why do i need this?

我之所以问是因为没有它们,我看不到任何明显的性能下降.这些命令通常仅在我使用以下命令存储结果时才使用吗?

Im asking because I do not see any noticeable performance degradation without them. Are those commands usually only used for when I store the result using:

$stmt->store_result();

赞:

$mysqli=new mysqli("database", "db", "pass", "user");

$stmt = $mysqli->prepare("SELECT email FROM users WHERE id=? ");
$stmt->bind_param('i',$_SESSION['id']);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($email);
while($stmt->fetch()){
     echo $email;
}
$stmt->free_result(); //why do i need this?
$stmt->close();       //why do i need this?

最终,问题归结到何时才是合适的时间使用freeresult()和close()?

Ultimately the question comes down to when is the appropriate time to use freeresult() and close()?

推荐答案

free_results语句告诉数据库引擎它可以释放结果集.

The free_results statement tells the database engine it can release the result set.

执行语句时,将创建一个迭代器.客户端(您的应用)通过逐个下载或分块下载来迭代每个结果.

When executing your statement, an iterator is created. The client (your app) iterates each result by either downloading them one by one or in chunk.

这使您的应用可以迭代数百万条记录,而无需一次下载所有结果.

This allows you app to iterate millions of record without downloading all the results in one chunk.

编辑

释放结果将释放客户端的内存.如果单个记录非常大且需要释放内存,则很有用.

Free result will free memory on the client side. Useful if a single record is very large and memory needs to be freed.

请参阅: http://php.net/manual/zh/function.mysql-free -result.php

这篇关于PHP :::预备语句::: freeresult()::: close()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆