[英]Why bindValue or BindParam doesn't modify the prepared statement?
[英]prepared statement using bindValue not working
我是PHP的新手,正在嘗試准備一個准備好的語句。 它是我在大學的最后一年的項目,我記得讀過,准備好的語句是很好的實踐,也適合SQL注入。 但是,以下代碼給了我Server 500錯誤。
<?php
$email = "blah@blah.co.uk";
$hash = "somerandomhashedpassword";
$db = new mysqli("localhost", "root", "1234", "UEAnetwork");
$sql = "INSERT INTO Students (Email, Password) VALUES (?,?)";
$stmt = $db->prepare($sql);
$stmt->bindValue(1, $email);
$stmt->bindValue(2, $hash);
if ($stmt->execute()) {
echo "You have registered!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
}
?>
如果運行以下命令,則會插入一行,因此,我很確定自己已正確連接到數據庫。
<?php
$db = new mysqli("localhost", "root", "1234", "UEAnetwork");
$sql = "INSERT INTO Students (Email, Password) VALUES ('blah@blah.co.uk','somerandomhashedpassword')";
$stmt = $db->prepare($sql);
if ($stmt->execute()) {
echo "You have registered!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
}
?>
我是否使用不正確的bindValue
? 我已經在許多在線教程中看到了這種用法,但是我一定做錯了。
mysqli
具有與PDO
完全不同的API。 沒有mysql_stmt::bindValue
。 您想使用mysql_stmt::bind_param
,但是語法卻大不相同:
$stmt->bind_param('ss', $email, $hash);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.