PHP mysqli_stmt_free_result() 函数用法及示例
mysqli_stmt_free_result()函数释放给定语句句柄的存储结果内存。
定义和用法
mysqli_stmt_free_result()函数接受(准备好的)语句对象作为参数,并释放存储给定语句结果的内存(使用mysqli_stmt_store_result()函数存储结果时)。
语法mysqli_stmt_free_result($stmt);
参数序号参数及说明
1con(必需)
这是表示准备好的语句的对象。
返回值
PHP mysqli_stmt_free_result()函数不返回任何值。
PHP版本
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
在线示例
以下示例演示了mysqli_stmt_free_result()函数的用法(面向过程风格),返回释放结果后的行数:
示例<?php
$con = mysqli_connect("localhost", "root", "password", "mydb");
mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
mysqli_query($con, "insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
print("创建表.....\n");
//读取记录
$stmt = mysqli_prepare($con, "SELECT * FROM Test");
//执行语句
mysqli_stmt_execute($stmt);
//存储结果
mysqli_stmt_store_result($stmt);
//行数
$count = mysqli_stmt_num_rows($stmt);
print("表中的行数: ".$count."\n");
//Freeing the resultset
mysqli_stmt_free_result($stmt);
$count = mysqli_stmt_num_rows($stmt);
print("释放结果后的行数: ".$count."\n");
//结束语句
mysqli_stmt_close($stmt);
//关闭连接
mysqli_close($con);
?>
输出结果创建表.....
表中的行数: 3
释放结果后的行数: 0
在线示例
在面向对象风格中,此函数的语法为$stmt->free_result();。以下是面向对象风格中此函数的示例;
示例<?php
//建立连接
$con = new mysqli("localhost", "root", "password", "mydb");
$con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
$con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
print("创建表.....\n");
$stmt = $con -> prepare( "SELECT * FROM Test");
//执行语句
$stmt->execute();
//存储结果
$stmt->store_result();
print("存储结果行数 ".$stmt ->num_rows);
//释放结果集内存
$stmt->free_result();
//结束语句
$stmt->close();
//关闭连接
$con->close();
?>
输出结果创建表.....
存储结果行数 : 3