我看到经常重复的评论"总是使用准备好的查询来防止SQL注入攻击".
使用准备好的查询和构造的查询之间的实际区别是什么,用户输入总是被清理?
建造
function quote($value) { global $db; return "'" . mysqli_real_escape_string($db, $value) . "'"; } $sql = "INSERT INTO foo (a, b) VALUES (" . quote($a) . "," . quote($b) . ")";
准备
$stmt = mysqli_prepare($db, "INSERT INTO foo (a, b) VALUES (?, ?)"); mysqli_stmt_bind_param($stmt, "ss", $a, $b);
除了冗长和风格之外,我还有什么理由想要使用另一个?