三個函數:
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);
兩種選擇:
1. 使用PDO
$stmt = $pdo->prepare('SELECT * FROM user WHERE name = :name');
$stmt->execute(array(':name' => $name));
foreach ($stmt as $row) {
// do something with $row
}
2. 使用mysqli
$stmt = $dbConnection->prepare('SELECT * FROM user WHERE name = ?');
$stmt->bind_param('s', $name);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// do something with $row
}