简体   繁体   English

bind_param()似乎不起作用

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

I have the following code: 我有以下代码:

<?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();
?>

When I execute the code, nothing gets added to the mysql database. 当我执行代码时,没有任何东西被添加到mysql数据库中。 But when I comment out the line 但是当我评论出这条线时

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

and insert the string and integer values directly into the $db->prepare statement (replacing the question marks), it all works nicely and the row is added to the database table. 并将字符串和整数值直接插入$ db-> prepare语句(替换问号),这一切都很好用,并将该行添加到数据库表中。

What am I doing wrong in the bind_param line that is preventing the new row being added to the database? 我在bind_param行中做错了什么,阻止将新行添加到数据库中?

mysqli_stmt_bind_param accepts variables (by reference). mysqli_stmt_bind_param接受变量(通过引用)。 You cannot use literals. 你不能使用文字。 Change your code to 将您的代码更改为

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

Please try the variable assignment after bind_param(). 请在bind_param()之后尝试变量赋值。 It is a passed by reference call. 它是通过引用调用传递的。 So it will work after also. 所以它也会起作用。

$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