php函数mysql_real_escape_string用于转义字符串中和SQL 有关的特殊字符,防止SQL注入攻击。
参数 | 描述 |
---|---|
string | 必需。规定要转义的字符串。 |
connection | 可选。规定 MySQL 连接。如果未规定,则使用上一个连接。 |
注意:如果没有连接MySQL就是用这个函数那么返回值总是false。
未连接MySQL
PHP代码:
$str = "list%%";
var_dump(mysql_real_escape_string($str));
var_dump(mysql_real_escape_string($str));
运行结果:
[root@localhost test]# phptest.php
PHP Warning: mysql_real_escape_string(): Accessdenied for user 'root'@'localhost' (using password: NO) in/var/www/html/test/test.php on line 23
Warning: mysql_real_escape_string(): Access denied for user'root'@'localhost' (using password: NO) in/var/www/html/test/test.php on line 23
PHP Warning: mysql_real_escape_string(): A linkto the server could not be established in/var/www/html/test/test.php on line 23
Warning: mysql_real_escape_string(): A link to the server could notbe established in /var/www/html/test/test.php on line 23
bool(false)
PHP Warning:
Warning: mysql_real_escape_string(): Access denied for user'root'@'localhost' (using password: NO) in/var/www/html/test/test.php on line 23
PHP Warning:
Warning: mysql_real_escape_string(): A link to the server could notbe established in /var/www/html/test/test.php on line 23
bool(false)
连接MySQL
PHP代码:
mysql_connect('192.168.193.129', 'root', 'miaohr1qaz');
$str = "list%%";
var_dump(mysql_real_escape_string($str));
$str = "list%%";
var_dump(mysql_real_escape_string($str));
运行结果:
[root@localhost test]# phptest.php
string(6) "list%%"
string(6) "list%%"