三个函数:
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
}