您的位置:首页 > 编程语言 > PHP开发

PHP mysql_real_escape_string的使用陷阱

2015-04-25 15:55 288 查看
php函数mysql_real_escape_string用于转义字符串中和SQL 有关的特殊字符,防止SQL注入攻击。

参数描述
string必需。规定要转义的字符串。
connection可选。规定 MySQL 连接。如果未规定,则使用上一个连接。
注意:如果没有连接MySQL就是用这个函数那么返回值总是false。

未连接MySQL
PHP代码:
$str = "list%%";

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)

连接MySQL
PHP代码:
mysql_connect('192.168.193.129', 'root', 'miaohr1qaz');

$str = "list%%";

var_dump(mysql_real_escape_string($str));

运行结果:
[root@localhost test]# phptest.php

string(6) "list%%"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: