Home / Learn Secure SQL / PHP: mysqli Prepared Statements

PHP: mysqli Prepared Statements

The alternative MySQL-specific driver, used the same safe way.

Bottom lineThe alternative MySQL-specific driver, used the same safe way.

Breaking it down

mysqli flow$mysqli->prepare($sql)$stmt->bind_param("s", $email)$stmt->execute()results fetched
mysqli flow

What it is

mysqli is PHP's MySQL-specific driver (as opposed to PDO's multi-database approach) and also supports prepared statements through prepare(), bind_param(), and execute().

Why it matters

Some older or MySQL-only codebases use mysqli rather than PDO; the same safe pattern applies, just with different method names.

Mechanically speaking

Call mysqli_prepare or $mysqli->prepare($sql), bind each value's type and content with bind_param, then execute().

Shows up in

  • Web application code review
  • Backend development
  • Database administration
PHP mysqli: positional parameters
<?php
$mysqli = new mysqli('localhost', $user, $pass, 'shop');
$stmt = $mysqli->prepare('SELECT * FROM users WHERE email = ?');
$stmt->bind_param('s', $_POST['email']);  // 's' = string type
$stmt->execute();
$result = $stmt->get_result();

Check yourself

1. bind_param's first argument specifies...