PHP mysqli_real_escape_string() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_real_escape_string() escapes special characters in a string so it can be safely placed inside a quoted SQL string literal, using the current connection charset.

It does not add quotes around the value — you must add them. Prepared statements are still preferred. mysqli_escape_string() is a related alias-style helper name in the catalog; prefer the real_escape_string name in new code.

Syntax

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

PHP
$safe = mysqli_real_escape_string($mysqli, $raw);
CallWhat it does
mysqli_real_escape_string($mysqli, "O'Reilly")Returns O\'Reilly (no surrounding quotes).
"'" . mysqli_real_escape_string($mysqli, $s) . "'"Builds a quoted SQL literal manually.
mysqli_set_charset($mysqli, "utf8mb4"); mysqli_real_escape_string($mysqli, $s)Sets charset first, then escapes.

A live connection is required because escaping depends on the connection character set. Always call mysqli_set_charset() after connect. For PHP 8.6+, mysqli_quote_string() can escape and quote in one step, but prepare/bind remains the best default.

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

Parameters

ParameterDescription
mysqlRequired. An open mysqli connection with the correct charset set.
stringRequired. The raw string to escape.

Return Value

Return ValueDescription
stringThe escaped string without added quotes.

PHP Examples

Example 1 — Escape a Quote Character

Show the escaped form of a name that contains an apostrophe.

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

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

mysqli_close($mysqli);

How It Works

  1. Connect and set utf8mb4 explicitly.
  2. real_escape_string encodes the apostrophe.
  3. The returned text still needs quotes when placed in SQL.
  4. Close the connection when done.

Example 2 — Build a Quoted Literal

Combine escape + quotes, then contrast with prepare.

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

$raw = "O'Reilly";
$literal = "'" . mysqli_real_escape_string($mysqli, $raw) . "'";
echo $literal, PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. Escape first.
  2. Wrap the escaped text in single quotes.
  3. The result is ready to embed as a string literal.
  4. Prepared statements avoid this manual work.

Example 3 — Charset First, Always

Demonstrate the required set_charset step before escaping.

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

if (!mysqli_set_charset($mysqli, "utf8mb4")) {
    echo "Charset failed.", PHP_EOL;
    exit;
}

$city = "München";
$escaped = mysqli_real_escape_string($mysqli, $city);
echo $escaped, PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. set_charset aligns PHP and MySQL encodings.
  2. Escaping then follows that charset.
  3. Without set_charset, multibyte edge cases can break escaping.
  4. Non-special characters may look unchanged, which is normal.

Frequently Asked Questions

It escapes special characters in a string for use inside a quoted SQL literal, using the connection charset.
No. You must add the surrounding quotes yourself, or use mysqli_quote_string() on PHP 8.6+.
Escaping depends on the connection character set. Call mysqli_set_charset() after connect before escaping.
Yes for application values. Bound parameters avoid manual escape mistakes.
mysqli_escape_string() is the older related name. Prefer mysqli_real_escape_string() in new code.

Next: mysqli_real_query()

mysqli_real_query() sends one SQL statement to MySQL and returns true if the server accept.

mysqli_real_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