mysqli::stmt_init

mysqli_stmt_init

(PHP 5, PHP 7, PHP 8)

mysqli::stmt_init -- mysqli_stmt_initInicializa uma instrução e retorna um objeto para uso com mysqli_stmt_prepare

Descrição

Estilo orientado a objetos

public mysqli::stmt_init(): mysqli_stmt|false

Estilo procedural

mysqli_stmt_init(mysqli $mysql): mysqli_stmt|false

Aloca e inicializa um objeto de instrução adequado para mysqli_stmt_prepare().

Nota:

Qualquer chamada subseqüente para qualquer função mysqli_stmt falhará até que mysqli_stmt_prepare() seja chamado.

Parâmetros

mysql

Somente no estilo procedural: Um objeto mysqli retornado por mysqli_connect() ou mysqli_init()

Valor Retornado

Retorna um objeto.

Veja Também

add a note add a note

User Contributed Notes 3 notes

up
13
Jeff C
7 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
9
mamdo7 at hotmail dot com
9 years ago
you can use $stmt = $mysqli->prepare(); directly without stmt-init() . i think there is no need for stmt-init .
up
-26
Deepak Sati
8 years ago
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.
To Top