PHP mysqli_stmt::free_result() / mysqli_stmt_free_result() 函数用于释放与语句,由 mysqli_stmt_store_result() 分配。
语法
//面向对象风格
public mysqli_stmt::free_result()
//面向过程风格
mysqli_stmt_free_result(statement)
参数
statement | 必填。 仅适用于面向过程风格:指定 mysqli_stmt_init() 返回的 mysqli_stmt 对象。 |
返回值
没有值
示例:面向对象风格
下面的示例展示了mysqli_stmt::free_result()方法的用法。
<?php
//建立与数据库的连接
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: ". $mysqli->connect_error;
exit();
}
//准备执行SQL语句
$query = "SELECT EmpID, Name, Age FROM Employee ORDER BY Age";
$stmt = $mysqli->prepare($query);
//执行SQL语句
$stmt->execute();
//将结果存储在内部缓冲区中
$stmt->store_result();
//获取语句中缓冲的行数
printf("Number of rows: %d \n", $stmt->num_rows);
//释放结果集内存
$stmt->free_result();
//结束语句
$stmt->close();
//关闭连接
$mysqli->close();
?>
上述代码的输出将类似于:
Number of rows: 58
示例:面向过程风格
下面的示例显示mysqli_stmt_free_result()的用法 函数。
<?php
//建立与数据库的连接
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: ". mysqli_connect_error();
exit();
}
//准备执行SQL语句
$query = "SELECT EmpID, Name, Age FROM Employee ORDER BY Age";
$stmt = mysqli_prepare($mysqli, $query);
//执行SQL语句
mysqli_stmt_execute($stmt);
//将结果存储在内部缓冲区中
mysqli_stmt_store_result($stmt);
//获取语句中缓冲的行数
printf("Number of rows: %d \n", mysqli_stmt_num_rows($stmt));
//释放结果集内存
mysqli_stmt_free_result($stmt);
//结束语句
mysqli_stmt_close($stmt);
//关闭连接
mysqli_close($mysqli);
?>
上述代码的输出将类似于:
Number of rows: 58