簡體   English   中英

bind_param()似乎不起作用

[英]bind_param() doesn't seem to work

我有以下代碼:

<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$db = new mysqli($dbhost, $dbuser, $dbpass, 'images_db');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
else{
echo "Connected to database";
}
//filename, mime_type and file_size are columns in the table images
$stmt = $db->prepare("INSERT INTO images (filename, mime_type, file_size) VALUES (?, ?, ?)");
$string1 = 'string 1';
$string2 = 'string 2';
$stmt->bind_param('ssi', $string1, $string2, 123);
$stmt->execute();
$stmt->close();
$mysqli->close();
?>

當我執行代碼時,沒有任何東西被添加到mysql數據庫中。 但是當我評論出這條線時

$stmt->bind_param('ssi', $string1, $string2, 123);

並將字符串和整數值直接插入$ db-> prepare語句(替換問號),這一切都很好用,並將該行添加到數據庫表中。

我在bind_param行中做錯了什么,阻止將新行添加到數據庫中?

mysqli_stmt_bind_param接受變量(通過引用)。 你不能使用文字。 將您的代碼更改為

$fileSize = 123;
$stmt->bind_param('ssi', $string1, $string2, $fileSize);

請在bind_param()之后嘗試變量賦值。 它是通過引用調用傳遞的。 所以它也會起作用。

$stmt = $db->prepare("INSERT INTO images (filename, mime_type, file_size) VALUES (?, ?, ?)");
$stmt->bind_param('ssi', $string1, $string2, $num);
$string1 = 'string 1';
$string2 = 'string 2';
$num=123;
$stmt->execute();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM