Example 1 — Prepare, Bind and Execute a SELECT
Show the standard safe pattern with a fixed sample name.
<?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
- mysqli_prepare() compiles the SQL template with one ?.
- bind_param attaches the integer id safely.
- execute runs the statement on the server.
- get_result + fetch_assoc reads the sample row name Ada.

