繁体   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