Funcții MySQL
PHP Manual

mysql_real_escape_string

(PHP 4 >= 4.3.0, PHP 5)

mysql_real_escape_stringEscapes special characters in a string for use in an SQL statement

Avertizare

Această extensie a devenit învechită începând cu PHP 5.5.0 și va fi eliminată în viitor. În locul ei trebuie utilizată extensia MySQLi sau PDO_MySQL. Accesați de asemenea ghidul MySQL: selectarea unei API și FAQ asociat pentru informații suplimentare. Variante alternative pentru această funcție includ:

Descrierea

string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier = NULL ] )

Escapes special characters in the unescaped_string, taking into account the current character set of the connection so that it is safe to place it in a mysql_query(). If binary data is to be inserted, this function must be used.

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.

This function must always (with few exceptions) be used to make data safe before sending a query to MySQL.

Precauţie

Security: the default character set

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

Parametri

unescaped_string

The string that is to be escaped.

link_identifier

Conexiunea MySQL. Dacă identificatorul legăturii nu este specificat, se presupune că este ultima legătură deschisă cu ajutorul mysql_connect(). Dacă nu este găsită nici o astfel de legătură, se va încerca crearea uneia prin apelul mysql_connect () fără argumente. În caz că nici o conexiune nu este găsită sau stabilită, se va genera o eroare de nivelul E_WARNING.

Valorile întoarse

Returns the escaped string, or FALSE on error.

Exemple

Example #1 Simple mysql_real_escape_string() example

<?php
// Connect
$link mysql_connect('mysql_host''mysql_user''mysql_password')
    OR die(
mysql_error());

// Query
$query sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
            
mysql_real_escape_string($user),
            
mysql_real_escape_string($password));
?>

Example #2 An example SQL Injection Attack

<?php
// We didn't check $_POST['password'], it could be anything the user wanted! For example:
$_POST['username'] = 'aidan';
$_POST['password'] = "' OR ''='";

// Query database to check if there are any matching users
$query "SELECT * FROM users WHERE user='{$_POST['username']}' AND password='{$_POST['password']}'";
mysql_query($query);

// This means the query sent to MySQL would be:
echo $query;
?>

The query sent to MySQL:

SELECT * FROM users WHERE user='aidan' AND password='' OR ''=''

This would allow anyone to log in without a valid password.

Note

Notă:

A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level E_WARNING is generated, and FALSE is returned. If link_identifier isn't defined, the last MySQL connection is used.

Notă:

If magic_quotes_gpc is enabled, first apply stripslashes() to the data. Using this function on data which has already been escaped will escape the data twice.

Notă:

If this function is not used to escape data, the query is vulnerable to SQL Injection Attacks.

Notă: mysql_real_escape_string() does not escape % and _. These are wildcards in MySQL if combined with LIKE, GRANT, or REVOKE.

A se vedea și


Funcții MySQL
PHP Manual