array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'pt_BR', ), 'this' => array ( 0 => 'function.mysql-real-escape-string.php', 1 => 'mysql_real_escape_string', ), 'up' => array ( 0 => 'ref.mysql.php', 1 => 'Funções da MySQL', ), 'prev' => array ( 0 => 'function.mysql-query.php', 1 => 'mysql_query', ), 'next' => array ( 0 => 'function.mysql-result.php', 1 => 'mysql_result', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/mysql/functions/mysql-real-escape-string.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4 >= 4.3.0, PHP 5)
mysql_real_escape_string — Escapes special characters in a string for use in an SQL statement
Esta extensão tornou-se defasada a partir do PHP 5.5.0 e foi removida no PHP 7.0.0. Em vez disso, as extensões MySQLi ou PDO_MySQL devem ser usadas. Veja também o guia MySQL: escolhendo uma API. Alternativas a esta função incluem:
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.
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.
unescaped_string
The string that is to be escaped.
link_identifier
A conexão MySQL. Se o
identificador da conexão não for especificado, a última conexão aberta por
mysql_connect() será usada. Se não houver uma conexão anterior,
haverá uma tentativa de criar uma como se mysql_connect() tivesse sido chamada
sem argumentos. Se nenhuma conexão for encontrada ou estabelecida, um erro de nível
E_WARNING
será gerado.
Returns the escaped string, or false
on error.
Executing this function without a MySQL connection present will
also emit E_WARNING
level PHP errors. Only
execute this function with a valid MySQL connection present.
Exemplo #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));
?>
Exemplo #2 mysql_real_escape_string() requires a connection example
This example demonstrates what happens if a MySQL connection is not present when calling this function.
<?php
// We have not connected to MySQL
$lastname = "O'Reilly";
$_lastname = mysql_real_escape_string($lastname);
$query = "SELECT * FROM actors WHERE last_name = '$_lastname'";
var_dump($_lastname);
var_dump($query);
?>
O exemplo acima produzirá algo semelhante a:
Warning: mysql_real_escape_string(): No such file or directory in /this/test/script.php on line 5 Warning: mysql_real_escape_string(): A link to the server could not be established in /this/test/script.php on line 5 bool(false) string(41) "SELECT * FROM actors WHERE last_name = ''"
Exemplo #3 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.
Nota:
A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level
E_WARNING
is generated, andfalse
is returned. Iflink_identifier
isn't defined, the last MySQL connection is used.
Nota:
If this function is not used to escape data, the query is vulnerable to SQL Injection Attacks.
Nota: mysql_real_escape_string() does not escape
%
and_
. These are wildcards in MySQL if combined withLIKE
,GRANT
, orREVOKE
.