防sql注入可用的函数有:
1.addslashes($string):用反斜线引用字符串中的特殊字符' " \
$username=addslashes($username);
2.mysql_escape_string($string):用反斜杠转义字符串中的特殊字符,用于mysql_query()查询
$username=mysql_escape_string($username);
3.mysql_real_escape_string($string):转义SQL语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集,需要保证当前是连接状态才能用该函数,否则会报警告, 不转义%与_
$username=mysql_real_escape_string($username);
例如:
$clean = array();
$mysql = array();
$clean['last_name'] = "O'Reilly";
$mysql['last_name'] = mysql_real_escape_string($clean['last_name']);
$sql = "INSERT
INTO user (last_name)
VALUES ('{$mysql['last_name']}')";
?>