防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']}')";
?>