php教程

mysqli::stmt_init

mysqli_stmt_init

(PHP 5, PHP 7)

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

说明

面向对象风格

mysqli::stmt_init ( void ) : mysqli_stmt

过程化风格

mysqli_stmt_init ( mysqli $link ) : mysqli_stmt

分配并初始化一个语句对象用于mysqli_stmt_prepare().

Note:

任何其后调用的mysqli_stmt函数都会失败直到mysqli_stmt_prepare()被调用.

参数

link

仅以过程化样式:由mysqli_connect()mysqli_init() 返回的链接标识。

返回值

Returns an object.

参见

User Contributed Notes

Jeff C 31-Aug-2016 09:32
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.
Deepak Sati 06-Jun-2015 11:56
If your sql query has some errors and you don't use stmt_init() before preparing the statement, you won't get error message for wrong sql query.
mamdo7 at hotmail dot com 07-Jun-2014 11:58
you can use $stmt = $mysqli->prepare(); directly without stmt-init() . i think there is no need for stmt-init .

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788