PHP Conference Nagoya 2025

mysqli::stmt_init

mysqli_stmt_init

(PHP 5, PHP 7, PHP 8)

mysqli::stmt_init -- mysqli_stmt_init初始化语句并返回用于 mysqli_stmt_prepare(调用)的对象

说明

面向对象风格

public mysqli::stmt_init(): mysqli_stmt|false

过程化风格

mysqli_stmt_init(mysqli $mysql): mysqli_stmt|false

分配并初始化适合 mysqli_stmt_prepare() 的语句对象。

注意:

在调用 mysqli_stmt_prepare() 之前,对其它 mysqli_stmt 函数的后续调用都会失败。

参数

mysql

仅以过程化样式:由 mysqli_connect()mysqli_init() 返回的 mysqli 对象。

返回值

返回对象。

参见

添加备注

用户贡献的备注 2 notes

up
19
Jeff C
8 years ago
stmt_init() seems to clear previous (possibly erroneous) results on the DB connection, which means you don't necessarily need to use it but it could make the code more robust.

In a PHPUnit test, I had a sequence of prepared queries on the same connection. One of them fetched a row from a SELECT but didn't keep fetching until it drained the connection, so it left some stale results. When the next query did this:

<?php
$db
= $this->getConnection()->getDbConnection();
$preparedQuery = $db->prepare ($query);
?>

the prepare() call generated an error: "Could not prepare query: Commands out of sync; you can't run this command now." Changing to this:

<?php
$db
= $this->getConnection()->getDbConnection();
$preparedQuery = $db->stmt_init();
$preparedQuery->prepare ($query);
?>

resolved the problem.
up
5
mamdo7 at hotmail dot com
10 years ago
you can use $stmt = $mysqli->prepare(); directly without stmt-init() . i think there is no need for stmt-init .
To Top