鑑於以下代碼,我需要轉義和消毒$city
?如果使用準備好的PHP/MySQL查詢,我需要清理輸入嗎?
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$city = "Amersfoort";
/* create a prepared statement */
if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {
/* bind parameters for markers */
$stmt->bind_param("s", $city);
/* execute query */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($district);
/* fetch value */
$stmt->fetch();
printf("%s is in district %s\n", $city, $district);
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
?>
使用準備好的查詢時是否需要清理任何輸入?
樹木擋在樹林中。 ;) – 2011-01-20 21:37:09
是的,但準備好的語句通常在應用程序層和數據層都需要更多的資源 - 當你看到有人說「總是使用準備好的語句」時要小心。 – Jmoney38 2013-09-24 11:41:05