PHP MySQLi 函数

PHP mysqli_result::free() / mysqli_result::close() / mysqli_result::free_result() / mysqli_free_result() 函数用于释放与结果关联的内存。

语法

//面向对象风格
public mysqli_result::free()
public mysqli_result::close()
public mysqli_result::free_result()

//面向过程风格
mysqli_free_result(result)

参数

结果 必填。 仅适用于面向过程风格:指定 mysqli_query()、mysqli_store_result()、mysqli_use_result() 或 mysqli_stmt_get_result() 返回的 mysqli_result 对象。

返回值

不返回值。

示例:面向对象风格

下面的示例展示了mysqli_result::free()的用法 方法。

<?php
//建立与数据库的连接
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//从数据库获取查询结果
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";
$result = $mysqli->query($sql);

//关闭连接
$mysqli->close();

//处理从数据库检索的数据
//- 将所有结果行作为关联数组获取
$rows = $result->fetch_all(MYSQLI_ASSOC);

//释放与结果相关的内存
$result->free();

//显示行
foreach ($rows as $row) {
  printf("%s, %d\n", $row["Name"], $row["Age"]);
}
?>

上述代码的输出将类似于:

Marry, 23
Kim, 26
John, 27
Adam, 28

示例:面向过程风格

示例下面显示了 mysqli_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 = "SELECT Name, Age FROM Employee ORDER BY Age";
$result = mysqli_query($mysqli, $sql);

//关闭连接
mysqli_close($mysqli);

//处理从数据库检索的数据
//- 将所有结果行作为关联数组获取
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);

//释放与结果相关的内存
mysqli_free_result($result);

//显示行
foreach ($rows as $row) {
  printf("%s, %d\n", $row["Name"], $row["Age"]);
}
?>

上述代码的输出将类似于:

Marry, 23
Kim, 26
John, 27
Adam, 28