English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_real_escape_string() Function Usage and Example

PHP MySQLi Manuale di riferimento

The mysqli_real_escape_string() function escapes special characters in SQL statements according to the character set of the current connection.

Definition and usage

mysqli_real_escape_string()The function is used to escape special characters in a string so that the string is a valid SQL statement. The input string will be escaped according to the character set of the current connection to get a valid SQL statement after encoding.

Syntax

mysqli_real_escape_string($con, $str)

Parameter

Serial numberParameters and descriptions
1

con (required)

This is an object representing the connection to the MySQL Server.

2

str (required)

This is a string in which you need to escape special characters.

Return value

mysqli_real_escape_string()Returns a string that can be used in a SQL query, that is, the escaped string.

Error/Exception

 Calling this function on an invalid connection will return NULL and emit a E_WARNING level error.

PHP version

This function was originally introduced in PHP version 5 and can be used in all higher versions.

Esempio online

The following examples demonstratemysqli_real_escape_string()Function usage (procedural style) -

// Establish connection
$con = mysqli_connect("localhost", "root", "password", "mydb");
//Crea la tabella
mysqli_query($con, "CREATE TABLE my_team(Name VARCHAR(255), Country VARCHAR(255))");
$player = "S'Dhawan";
$country = "India";
//Inserisci il record
$res = mysqli_query($con, "INSERT into my_team VALUES ('$player', '$country')");
if (!$res){
   print("Si è verificato un errore");
}else{
   print("Inserimento record riuscito");
}
print("\n");
$player = mysqli_real_escape_string($con, $player);
$country = mysqli_real_escape_string($con, $country);
//Inserisci il record
$res = mysqli_query($con, "INSERT into my_team VALUES ('$player', '$country')");
if (!$res){
   print("Si è verificato un errore");
}else{
   print("Inserimento record riuscito");
}
//Chiudi la connessione
mysqli_close($con);
?>

Output del risultato

Si è verificato un errore
Inserimento record riuscito

Esempio online

Nel estilo orientato agli oggetti, la sintassi di questa funzione è}}$con->real_escape_string();。Ecco un esempio di funzione in stile orientato agli oggetti;

<?php
   //Connettiti al database
   $con = new mysqli("localhost", "root", "password", "test");
   //Crea la tabella
   $con->query("CREATE TABLE my_team(Name VARCHAR(255), Country VARCHAR(255))");
   $player = "S'Dhawan";
   $country = "India";
   //Inserisci il record
   $res = $con->query("INSERT into my_team VALUES ('$player')");
   if (!$res){
       print("Si è verificato un errore");
   }else{
       print("Inserimento record riuscito");
   }
   print("\n");
   $player = $con->real_escape_string($player);
   //Inserisci il record
   $res = $con->query("INSERT into my_team (Name) VALUES ('$player')");
   if (!$res){
       print("Si è verificato un errore");
   }else{
       print("Inserimento record riuscito");
   }
   //Chiudi la connessione
   mysqli_close($con);
?>

Output del risultato

Si è verificato un errore
Inserimento record riuscito

Esempio online

Esempio di escape di caratteri speciali di stringa singola:

<?php
   $con = mysqli_connect("localhost","root","password","mydb");
   
   if (mysqli_connect_errno($con)){
      echo "Connessione MySQL fallita: \" . mysqli_connect_error();
   }
   $myName = "Jr's";
   $myName = mysqli_real_escape_string($con,$myName);
   
   mysqli_query($con,"INSERT into emp (name) VALUES ('$myName')");
   mysqli_close($con);

PHP MySQLi Manuale di riferimento