mysqli::real_escape_string

mysqli_real_escape_string

(PHP 5, PHP 7, PHP 8)

mysqli::real_escape_string -- mysqli_real_escape_stringEscapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection

Beschreibung

Objektorientierter Stil

public mysqli::real_escape_string(string $string): string

Prozeduraler Stil

mysqli_real_escape_string(mysqli $mysql, string $string): string

This function is used to create a legal SQL string that you can use in an SQL statement. The given string is encoded to produce an escaped SQL string, taking into account the current character set of the connection.

Achtung

Security: the default character set

The character set must be set either at the server level, or with the API function mysqli_set_charset() for it to affect mysqli_real_escape_string(). See the concepts section on character sets for more information.

Parameter-Liste

mysql

Nur bei prozeduralem Aufruf: Ein von mysqli_connect() oder mysqli_init() zurückgegebenes mysqli-Objekt.

string

The string to be escaped.

Characters encoded are NUL (ASCII 0), \n, \r, \, ', ", and Control-Z.

Rückgabewerte

Returns an escaped string.

Beispiele

Beispiel #1 mysqli::real_escape_string() example

Objektorientierter Stil

<?php

mysqli_report
(MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost""my_user""my_password""world");

$city "'s-Hertogenbosch";

/* this query with escaped $city will work */
$query sprintf("SELECT CountryCode FROM City WHERE name='%s'",
    
$mysqli->real_escape_string($city));
$result $mysqli->query($query);
printf("Select returned %d rows.\n"$result->num_rows);

/* this query will fail, because we didn't escape $city */
$query sprintf("SELECT CountryCode FROM City WHERE name='%s'"$city);
$result $mysqli->query($query);

Prozeduraler Stil

<?php

mysqli_report
(MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT);
$mysqli mysqli_connect("localhost""my_user""my_password""world");

$city "'s-Hertogenbosch";

/* this query with escaped $city will work */
$query sprintf("SELECT CountryCode FROM City WHERE name='%s'",
    
mysqli_real_escape_string($mysqli$city));
$result mysqli_query($mysqli$query);
printf("Select returned %d rows.\n"mysqli_num_rows($result));

/* this query will fail, because we didn't escape $city */
$query sprintf("SELECT CountryCode FROM City WHERE name='%s'"$city);
$result mysqli_query($mysqli$query);

Die oben gezeigten Beispiele erzeugen eine ähnliche Ausgabe wie:

Select returned 1 rows.

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's-Hertogenbosch'' at line 1 in...

Siehe auch

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Kategorie: Software & Web-Development

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

SEVENFRIDAY replica

New SEVENFRIDAY ME1/02 ME1/02, featuring a green as well as gold color scheme. swiss replica watches (https://www.whereguidewatch.com) Cas ...

Geschrieben von enyar am 18.03.2026 09:46:36
Forum: PHP Developer Forum
replica Greubel Forsey Balancier Convexe S²

{"data-align":"none","data-size":"full","src":"https:\/\/www.reviewluxurystore.com\/images\/Greubel%20Forsey%20Watch%20Balancier%20Convexe%20S2%20 ...

Geschrieben von enyar am 18.03.2026 09:45:01
Forum: PHP Developer Forum
MSQL auslesen // Tabelle, longtext, utf8mb4_unicode_520_ci

Alle Formulareinträge werden in einer Tabelle gespeichert; du Stickman Hook Game (https://stickman-hookgame.io/)​ kannst die Felder (Name, E-Ma ...

Geschrieben von burlingtonrealtor am 18.03.2026 05:20:52
Forum: SQL / Datenbanken
SEO-URLs und MySQL

When people talk about “SEO-friendly URLs” in the context of MySQL, they’re really talking about how you store and retrieve URL slugs effici ...

Geschrieben von rickluck90 am 17.03.2026 21:52:43
Forum: SQL / Datenbanken