什么时候关闭 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/