English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
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.
mysqli_real_escape_string($con, $str)
Serial number | Parameters 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. |
mysqli_real_escape_string()Returns a string that can be used in a SQL query, that is, the escaped string.
Calling this function on an invalid connection will return NULL and emit a E_WARNING level error.
This function was originally introduced in PHP version 5 and can be used in all higher versions.
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
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 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);