gpt4 book ai didi

php - bind_param() 似乎不起作用

转载 作者:可可西里 更新时间:2023-11-01 00:00:27 24 4
gpt4 key购买 nike

我有以下代码:

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

关于php - bind_param() 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18710437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com