php提供了3個函數來避免sql注入,分別是:
1.addslashes()用于單字節字符串的處理,他是強行加/
$username=addslashes($username);
2.mysql_escape_string用于多字節字符串的處理,不考慮連接的當前字符集
$username=mysql_escape_string($username);
3.mysql_real_escape_string()用于多字節字符串的處理,會判斷字符集,但是對PHP版本有要求
示例:
$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']}')";
?>