bind_param()似乎不起作用 [英] bind_param() doesn't seem to work
本文介绍了bind_param()似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
<?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数据库中.但是当我注释掉这一行
When I execute the code, nothing gets added to the mysql database. But when I comment out the line
$stmt->bind_param('ssi', $string1, $string2, 123);
并将字符串和整数值直接插入$ db-> prepare语句(替换问号)中,一切正常,并将行添加到数据库表中.
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.
bind_param行中发生了什么错误,阻止了将新行添加到数据库中?
What am I doing wrong in the bind_param line that is preventing the new row being added to the database?
推荐答案
mysqli_stmt_bind_param
接受变量(通过引用).您不能使用文字.将您的代码更改为
mysqli_stmt_bind_param
accepts variables (by reference). You cannot use literals. Change your code to
$fileSize = 123;
$stmt->bind_param('ssi', $string1, $string2, $fileSize);
这篇关于bind_param()似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文