PHP mysqli_prepare() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_prepare() sends an SQL template with ? placeholders to MySQL and returns a mysqli_stmt object, or false on failure.

After prepare you bind values (bind_param), execute, then fetch results from the statement. Prefer prepare over building SQL with string concatenation. For quick static SQL with no user values, mysqli_query() is fine.

Syntax

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

PHP
$stmt = mysqli_prepare($mysqli, "SELECT id, name FROM users WHERE id = ?");
CallWhat it does
mysqli_prepare($mysqli, "SELECT name FROM users WHERE id = ?")Returns a mysqli_stmt or false.
mysqli_prepare($mysqli, "INSERT INTO users (name) VALUES (?)")Prepares an INSERT template with one placeholder.
if ($stmt = mysqli_prepare($mysqli, $sql)) { /* bind + execute */ }Guards against prepare failure before binding.

High-level flow: prepare → bind_param → execute → (get_result / bind_result + fetch) → close. Placeholders are for values only, not table or column names. Mentally link prepare to query: query runs finished SQL now; prepare makes a reusable safe template for values.

Library declarations
PHP
mysqli_prepare(mysqli $mysql, string $query): mysqli_stmt|false

Parameters

ParameterDescription
mysqlRequired. The open mysqli connection.
queryRequired. The SQL template. Use ? where each bound value will go.

Return Value

Return ValueDescription
mysqli_stmt|falseA statement object on success, or false if prepare failed.

PHP Examples

Example 1 — Prepare, Bind and Execute a SELECT

Show the standard safe pattern with a fixed sample name.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");

$stmt = mysqli_prepare($mysqli, "SELECT name FROM users WHERE id = ?");
$id = 42;
mysqli_stmt_bind_param($stmt, "i", $id);
mysqli_stmt_execute($stmt);

$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
echo $row["name"], PHP_EOL;

mysqli_stmt_close($stmt);
mysqli_close($mysqli);

How It Works

  1. mysqli_prepare() compiles the SQL template with one ?.
  2. bind_param attaches the integer id safely.
  3. execute runs the statement on the server.
  4. get_result + fetch_assoc reads the sample row name Ada.

Example 2 — Prepare Failure Returns False

A bad template does not return a statement object.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");

$stmt = mysqli_prepare($mysqli, "SELEC name FROM users WHERE id = ?");
echo $stmt === false ? "Prepare failed." : "Unexpected success.", PHP_EOL;
echo mysqli_error($mysqli), PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. The typo SELEC makes prepare fail.
  2. The return value is false.
  3. mysqli_error() explains the syntax problem.
  4. Do not call bind_param on false.

Example 3 — When query() Is Enough

Static SQL without user values can stay on mysqli_query().

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");

$result = mysqli_query($mysqli, "SELECT COUNT(*) AS c FROM users");
$row = mysqli_fetch_assoc($result);
echo "Users: ", $row["c"], PHP_EOL;

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. No variable values appear in the SQL.
  2. mysqli_query() runs it directly.
  3. Use prepare when values come from forms, URLs or APIs.
  4. Both APIs share the same connection object.

Frequently Asked Questions

A mysqli_stmt on success, or false on failure.
Bind values with mysqli_stmt_bind_param(), then mysqli_stmt_execute(), then fetch results.
query() runs a finished SQL string now. prepare() creates a template so values can be bound safely and reused.
No. Placeholders are for data values only. Identifiers must be validated separately.
Not for bound parameters. Binding replaces manual escaping for those values.

Next: mysqli_query()

mysqli_query() runs one SQL statement on the connection and returns a mysqli_result for SE.

mysqli_query() 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