我正在研究mysqli准备的陈述,我对此有两个问题。
当我阅读时,我发现准备好的语句的执行顺序如下所示:
$sql = 'SELECT image_id, filename, caption FROM images WHERE image_id = ?';
// connect to the database
$conn = ....
$stmt = $conn->stmt_init();
$stmt->prepare($sql);
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($image_id, $filename, $caption);
// optional: get total of records in the result set
$stmt->store_result();
$numRows = $stmt->num_rows;
// loop through the result set
while ($stmt->fetch()) {
// code goes here...
}
or
// fetch the result for one record
$stmt->fetch()
// free & close
$stmt->free_result();
$stmt->close;
$conn->close();这是我的第一个问题:
在我阅读时,它还提到了以下内容:
如果不将结果绑定到变量,请使用$row = $stmt->fetch(),并将每个变量作为$row‘’column_name‘访问。所以,
他们是否使用这两种方法中的任何一种来循环结果集?如果没有区别,那么为什么要首先使用$stmt->bind_result绑定结果呢?如果我可以使用$row = $stmt->fetch(),那又有什么意义呢?
我的另一个问题是:
希望你的回答能让我明白更好的陈述,这样我就能建立安全的东西.
谢谢
发布于 2012-09-20 08:15:22
尽管手册声明:“当不再需要结果对象时,您应该始终使用mysqli_free_result()释放结果”,但通常的做法是不使用free_result并关闭语句。当关闭时,您不能再使用结果集,或者重用它,当php死掉时,内存无论如何都会被释放。
https://stackoverflow.com/questions/12508389
复制相似问题