防sql注入可用的函數(shù)有:
1.addslashes($string):用反斜線引用字符串中的特殊字符' " \
$username=addslashes($username);
2.mysql_escape_string($string):用反斜杠轉(zhuǎn)義字符串中的特殊字符,用于mysql_query()查詢
$username=mysql_escape_string($username);
3.mysql_real_escape_string($string):轉(zhuǎn)義SQL語句中使用的字符串中的特殊字符,并考慮到連接的當(dāng)前字符集,需要保證當(dāng)前是連接狀態(tài)才能用該函數(shù),否則會報警告, 不轉(zhuǎn)義%與_
$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']}')";
?>