PHP mysqli_stmt::close() / mysqli_stmt_close() 函数用于关闭准备好的语句。它还释放语句句柄。如果当前语句有待处理或未读结果,该函数将取消它们,以便执行下一个查询。
语法
//面向对象风格
public mysqli_stmt::close()
//面向过程风格
mysqli_stmt_close(statement)
参数
statement | 必填。 仅适用于面向过程风格:指定 mysqli_stmt_init() 返回的 mysqli_stmt 对象。 |
返回值
返回 true成功或失败时返回 false。
示例:面向对象风格
下面的示例显示了 mysqli_stmt::close() 方法的用法。
<?php
//建立与数据库的连接
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: ". $mysqli->connect_error;
exit();
}
//创建准备好的语句
$stmt = $mysqli->stmt_init();
$query = "INSERT INTO Employee (Name, City, Salary) VALUES (?, ?, ?)";
$stmt->prepare($query);
//绑定参数
$stmt->bind_param('ssd', $name, $city, $salary);
//设置参数并执行
$name = "John";
$city = "London";
$salary = 2800;
$stmt->execute();
$name = "Marry";
$city = "Paris";
$salary = 2850;
$stmt->execute();
echo "Records inserted successfully.";
//结束语句
$stmt->close();
//关闭连接
$mysqli->close();
?>
上述代码的输出将类似于:
Records inserted successfully.
示例:面向过程风格
下面的示例显示了 的用法mysqli_stmt_close() 函数。
<?php
//建立与数据库的连接
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: ". mysqli_connect_error();
exit();
}
//创建准备好的语句
$stmt = mysqli_stmt_init($mysqli);
$query = "INSERT INTO Employee (Name, City, Salary) VALUES (?, ?, ?)";
mysqli_stmt_prepare($stmt, $query);
//绑定参数
mysqli_stmt_bind_param($stmt, 'ssd', $name, $city, $salary);
//设置参数并执行
$name = "John";
$city = "London";
$salary = 2800;
mysqli_stmt_execute($stmt);
$name = "Marry";
$city = "Paris";
$salary = 2850;
mysqli_stmt_execute($stmt);
echo "Records inserted successfully.";
//结束语句
mysqli_stmt_close($stmt);
//关闭连接
mysqli_close($mysqli);
?>
上述代码的输出将类似于:
Records inserted successfully.