PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

PHP mysql_real_escape_string的一处注意_PHP教程

原创
2016-07-13 10:33:47 854浏览

mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符。

下列字符受影响:

  • x00
  • n
  • r
  • '
  • "
  • x1a

如果成功,则该函数返回被转义的字符串。如果失败,则返回 false。

用法为mysql_real_escape_string(string,connection)

  • 参数string,必需。规定要转义的字符串。
  • 参数connection,可选。规定 MySQL 连接。如果未规定,则使用上一个连接。

本函数将 string 中的特殊字符转义,并考虑到连接的当前字符集,因此可以安全用于 mysql_query()。使用本函数来预防数据库攻击。

mysql_real_escape_string()的简单使用

SQL被注入的情况(没有使用mysql_real_escape_string())

数据库攻击。本例演示如果我们不对用户名和密码应用 mysql_real_escape_string() 函数会发生什么:

那么 SQL 查询会成为这样:

SELECT * FROM users
WHERE user='john' AND password='' OR ''=''

这意味着任何用户无需输入合法的密码即可登陆。

预防数据库攻击的正确做法:

一些经验

mysql_escape_string()也起到差不多的效果。关于这两个函数,记得我在用mysql_real_escape_string() 这个函数时,程序总是出错,不知道错误的原因,后来换成mysql_escape_string() 这个函数时,就可以了。查找手册,发现在用mysql_real_escape_string() 时,必须要先建立数据库连接,否则则报错 -___-|||

mysql_real_escape_string() calls MySQL's library function mysql_escape_string, which prepends backslashes to the following characters: NULL, x00, n, r, , ', " and x1a

mysql_escape_string() does not escape % and _.

This function is identical to mysql_real_escape_string() except that mysql_real_escape_string() takes a connection handler and escapes the string according to the current character set. mysql_escape_string() does not take a connection argument and does not respect the current charset setting.

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/752406.htmlTechArticlemysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符。 下列字符受影响: x00 n r ' x1a 如果成功,则该函数返回被转义的字符...
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。