mysql_real_escape_string

(PHP 4 >= 4.3.0, PHP 5)

mysql_real_escape_string ת�� SQL �����ʹ�õ��ַ����е������ַ��������ǵ����ӵĵ�ǰ�ַ���

Warning

����չ�� PHP 5.5.0 ���ѷ����������� PHP 7.0.0 ��ʼ���Ƴ���Ӧʹ�� MySQLi �� PDO_MySQL ��չ���滻֮���μ� MySQL��ѡ�� API ָ���Լ���� FAQ ����ȡ������Ϣ������������������У�

˵��

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

�������� unescaped_string �е������ַ�ת�壬���Ƽ����ӵĵ�ǰ�ַ�������˿��԰�ȫ���� mysql_query()��

mysql_real_escape_string() ����mysql��ĺ��� mysql_real_escape_string, �������ַ�ǰ��ӷ�б��: \x00, \n, \r, \, ', " �� \x1a.

Ϊ�˰�ȫ���������MySQL���Ͳ�ѯǰ�������������������������������������

Caution

��ȫ��ʾ: Ĭ���ַ���

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

MySQL ���ӡ��粻ָ�����ӱ�ʶ����ʹ���� mysql_connect() ����򿪵����ӡ����û���ҵ������ӣ��᳢�Բ����������� mysql_connect() ����������û���ҵ����ӻ��޷��������ӣ�������� E_WARNING ����Ĵ���

����ֵ

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.

����

Example #1 mysql_real_escape_string() ����

<?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 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);
?>

�������̵���������ڣ�

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 = ''"

Example #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.

ע��

Note:

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.

Note:

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.

Note:

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

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

�μ�