php - 何时关闭 Prepared Statement

标签 php prepared-statement

什么时候关闭 PHP 中的准备语句?

例子:

    $query = "insert into web_reviews (title,added_date,reviewer_home_url,read_more_link,summary) values(?,?,?,?,?)";
    $stmt = $this->db->prepare($query);
    $stmt->bind_params($this->title,$this->added_date,$this->reviewer_home_url,$this->read_more,$this->summary);
    $stmt->execute() or die("Cannot add the date to the database, please try again.");
    $stmt->close();

    $stmt = $this->db->prepare("select id from web_reviews where title = ? and read_more = ?");
    $stmt->bind_params($this->title,$this->read_more);
    $stmt->execute();
    $stmt->bind_results($web_review_id);
    $stmt->close();

我应该在这里使用 $stmt->close(); 吗?

编辑:

PHP 手册上的内容以及手册中的一条评论说:

Closes a prepared statement. mysqli_stmt_close() also deallocates the statement handle. If the current statement has pending or unread results, this function cancels them so that the next query can be executed.

评论:

if you are repeating an statement in an loop using bind_param and so on inside it for a larger operation. i thougt id would be good to clean it with stmt->close. but it broke always with an error after aprox. 250 operations . As i tried it with stmt->reset it worked for me.

最佳答案

这是 close 的一个很好的用法,尤其是当您计划进行另一个查询时。对于 PDO 语句和 MySQLi 语句,我发现在清洁方面犯错几乎总是最好的——它消除了潜在的错误。

至于有 250 次操作的先生们……我看不出真正的用例是什么。为什么他需要查询数据库 250 次不同的时间?为什么他不能用250条记录查询一次数据库?或者,更可能的是,他为什么不能用 10 条记录查询数据库 25 次?

关于php - 何时关闭 Prepared Statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6631364/

相关文章:

php - 在 PHP 中,NULL 和将字符串设置为等于 2 个单引号之间有什么区别

php - 通过参数值限制对路由的访问

php - jquery 重新填充表表单字段

php - 我需要如何返回查询才能计算行数,以及如何计算行数?

java - 从 Java PreparedStatement 中的字段调用函数?

php - 在PHP中通过IP阻止客户端一段时间

php - 自动增量与唯一键的区别当你把它们都放在一张表中时?

php - 存在撇号时使用 json_encode 破坏 JSON 对象

java - Java 中非常奇怪的 PreparedStatement?

php - mysqli_fetch_assoc()期望参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?