我们利用了php自带的Magic Quotes来判断是否是开启了,如果是就stripslashes否则就用mysql_real_escape_string来过滤,实例代码如下:
-
-
- if (get_magic_quotes_gpc()) {
- $name = stripslashes($name);
- }else{
- $name = mysql_real_escape_string($name);
- }
- mysql_query("SELECT * FROM users WHERE name='{$name}'");
注:mysql_real_escape_string函数要等到mysql数据库连接成功才有效. |