mysql_real_escape_string

(PHP 4 >= 4.3.0, PHP 5)

mysql_real_escape_string 轉義 SQL 語句中使用的字符串中的特殊字符,并考慮到連接的當前字符集

警告

本擴展自 PHP 5.5.0 起已廢棄,并在自 PHP 7.0.0 開始被移除。應使用 MySQLiPDO_MySQL 擴展來替換之。參見 MySQL:選擇 API 指南來獲取更多信息。用以替代本函數(shù)的有:

說明

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

本函數(shù)將 unescaped_string 中的特殊字符轉義,并計及連接的當前字符集,因此可以安全用于 mysql_query()。

mysql_real_escape_string() 調用mysql庫的函數(shù) mysql_real_escape_string, 在以下字符前添加反斜杠: \x00, \n, \r, \, ', "\x1a.

為了安全起見,在像MySQL傳送查詢前,必須調用這個函數(shù)(除了少數(shù)例外情況)。

警告

安全提示: 默認字符集

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.

參數(shù)

unescaped_string

The string that is to be escaped.

link_identifier

MySQL 連接。如不指定連接標識,則使用由 mysql_connect() 最近打開的連接。如果沒有找到該連接,會嘗試不帶參數(shù)調用 mysql_connect() 來創(chuàng)建。如沒有找到連接或無法建立連接,則會生成 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.

范例

示例 #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));
?>

示例 #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 = ''"

示例 #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.

注釋

注意:

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.

注意:

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.

注意:

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

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

參見