PHP mysqli_stmt_init() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_stmt_init() creates a blank statement object that you later prepare with mysqli_stmt_prepare().

Most beginners should call mysqli_prepare() instead, which init+prepare in one step and returns mysqli_stmt|false. Use stmt_init() when you need the two-step procedural style or older examples that separate init from prepare.

Syntax

Call mysqli_stmt_init() with the values you need. Start with this line.

PHP
$stmt = mysqli_stmt_init($mysql);
CallWhat it does
mysqli_stmt_init($link)Returns a mysqli_stmt handle ready to be prepared, or false on failure.
mysqli_prepare($link, 'SELECT id FROM users WHERE id = ?')Preferred one-step alternative for most code.
$mysqli->stmt_init()Object-oriented form that still needs prepare afterward.

stmt_init() alone does not send SQL. You must prepare (and usually bind/execute) next. Object style often prefers $mysqli->prepare($sql) directly.

Library declarations
PHP
mysqli_stmt_init(mysqli $mysql): mysqli_stmt|false

Parameters

ParameterDescription
mysqlRequired. Procedural style only: an open mysqli connection.

Return Value

Return ValueDescription
mysqli_stmtAn empty statement object for mysqli_stmt_prepare().
falseInitialization failed.

PHP Examples

Example 1 — Init Then Prepare

Two-step flow with fixed demo messages.

PHP
<?php
echo 'stmt = mysqli_stmt_init($link)', PHP_EOL;
echo 'mysqli_stmt_prepare($stmt, 'SELECT id FROM users WHERE id = ?')', PHP_EOL;
echo 'then bind + execute', PHP_EOL;

How It Works

  1. mysqli_stmt_init() allocates the statement object.
  2. mysqli_stmt_prepare() attaches the SQL with placeholders.
  3. Bind parameters and execute next.
  4. Sample output when connected to a demo database is a successful prepare/execute.

Example 2 — Why Prefer prepare()

Contrast the one-step helper beginners should learn first.

PHP
<?php
echo 'preferred: $stmt = mysqli_prepare($link, $sql);', PHP_EOL;
echo 'verbose:   $stmt = mysqli_stmt_init($link); mysqli_stmt_prepare($stmt, $sql);', PHP_EOL;

How It Works

  1. mysqli_prepare() wraps init+prepare for you.
  2. stmt_init() remains valid for older or split-step code.
  3. Both end at a mysqli_stmt you can bind and execute.
  4. Teach prepare() first unless a lesson specifically covers stmt_init().

Example 3 — OO stmt_init()

Object style still needs a prepare call afterward.

PHP
<?php
class DemoMysqli {
    public function stmt_init() {
        return (object) ['ready' => true];
    }
}
$mysqli = new DemoMysqli();
$stmt = $mysqli->stmt_init();
echo !empty($stmt->ready) ? 'stmt ready for prepare' : 'fail', PHP_EOL;

How It Works

  1. $mysqli->stmt_init() returns a statement object.
  2. Next call prepare on that statement with your SQL.
  3. Or skip straight to $mysqli->prepare($sql).
  4. Never concat user input into the SQL string.

Frequently Asked Questions

It creates an empty mysqli_stmt tied to a connection, before you prepare SQL on it.
prepare() creates and prepares in one step. stmt_init() only creates; you must prepare separately.
mysqli_prepare() or $mysqli->prepare() is clearer for most tutorials.
No. It only allocates a statement object.
A mysqli_stmt on success, or false on failure.

Next: mysqli_store_result()

mysqli_store_result() transfers the full result set of the previous query into a buffered .

mysqli_store_result() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful