Home > Backend Development > PHP Tutorial > php中转义mysql语句的实现代码_PHP

php中转义mysql语句的实现代码_PHP

WBOY
Release: 2016-06-01 12:15:42
Original
884 people have browsed it

你总不可能对每一个这样的特殊字符都人工进行转义,何况你通常处理的都是表单自动提交的内容。

所以,应该使用mysql_real_escape_string函数:

mysql_real_escape_string — 转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集。

但是注意:该函数并不转义 % 和 _。另外,最好不要对整条sql语句使用该函数,而是只转义传入sql语句的字符串参数,否则会发生意想不到的结果。

脚本举例:
复制代码 代码如下:
$item = "Zak's and Derick's Laptop";
$escaped_item = mysql_real_escape_string($item);
printf ("Escaped string: %s\n", $escaped_item);
?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template