PHP mysqli_escape_string() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_escape_string() escapes special characters in a string for use in a SQL statement on that connection; it is an alias of mysqli_real_escape_string().

Object-oriented style uses $mysqli->real_escape_string($string) (there is no separate escape_string method name in common use). Escaping depends on the connection character set, so call set_charset() first. Prefer prepared statements instead of building SQL with escaped strings.

Syntax

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

PHP
$safe = mysqli_escape_string($mysqli, $value);
CallWhat it does
mysqli_escape_string($mysqli, "O'Reilly")Escapes the quote for safe inclusion in SQL.
mysqli_real_escape_string($mysqli, $value)The canonical function name; escape_string is an alias.
$mysqli->real_escape_string($value)Object-oriented escaping method.

mysqli_escape_string() and mysqli_real_escape_string() are the same. Escaping is not enough if you forget quotes around values, and it is easy to misuse. Prepared statements with bind parameters are the safer default.

Library declarations
PHP
mysqli_escape_string(mysqli $mysql, string $string): string

Parameters

ParameterDescription
mysqlRequired. The mysqli connection (charset must already be set).
stringRequired. The string value to escape.

Return Value

Return ValueDescription
stringThe escaped string ready for careful inclusion in a quoted SQL literal.

PHP Examples

Example 1 — Escape an Apostrophe

Show how quotes are escaped. Sample output when connected to a demo database.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
mysqli_set_charset($mysqli, "utf8mb4");

$raw = "O'Reilly";
$safe = mysqli_escape_string($mysqli, $raw);
echo $safe, PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. set_charset('utf8mb4') sets the encoding used for escaping.
  2. The raw name contains an apostrophe.
  3. mysqli_escape_string() aliases real_escape_string and escapes it.
  4. Sample output shows a backslash before the quote.

Example 2 — Alias Equals real_escape_string

Prove both function names return the same text.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
$mysqli->set_charset("utf8mb4");
$value = 'a\b"c';

$a = mysqli_escape_string($mysqli, $value);
$b = mysqli_real_escape_string($mysqli, $value);
echo $a === $b ? "same" : "different", PHP_EOL;
echo $a, PHP_EOL;

$mysqli->close();

How It Works

  1. A string with backslashes and quotes is prepared.
  2. Both escape functions run on the same connection.
  3. Identical results confirm the alias relationship.
  4. The escaped form is printed for inspection.

Example 3 — Prefer a Prepared Statement

Safer pattern: bind the value instead of escaping into SQL text.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
mysqli_set_charset($mysqli, "utf8mb4");

$name = "O'Reilly";
$stmt = mysqli_prepare($mysqli, "SELECT id FROM products WHERE name = ?");
mysqli_stmt_bind_param($stmt, "s", $name);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id);
mysqli_stmt_fetch($stmt);
echo "id=", $id, PHP_EOL;

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

How It Works

  1. A placeholder ? stands in for the name value.
  2. bind_param sends the string safely without manual escaping.
  3. execute + fetch reads the demo row.
  4. Sample output when connected to a demo database shows id=7.

Frequently Asked Questions

Yes. mysqli_escape_string() is an alias of mysqli_real_escape_string(). Prefer the real_escape_string name in new code, or better, use prepared statements.
Escaping rules depend on the connection character set. Call set_charset('utf8mb4') before escaping so multi-byte characters are handled correctly.
It helps only when you correctly place the escaped value inside quotes. Prepared statements are safer and clearer, so prefer them.
Use $mysqli->real_escape_string($string). That is the method form of the real_escape_string / escape_string function pair.
Learn prepared statements (or execute_query with parameters) first. Treat escaping as a legacy technique you may see in older code.

Next: mysqli_execute_query()

mysqli_execute_query() prepares, binds, executes and (for SELECT) returns a result in one .

mysqli_execute_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