【问题标题】:bind_param() doesn't seem to workbind_param() 似乎不起作用
【发布时间】:2013-09-13 15:53:57
【问题描述】:

我有以下代码:

<?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 行中做错了什么?

【问题讨论】:

  • 做 printf("错误: %s.\n", $stmt->error);在您的 execute() 之后查看是否有任何错误..!
  • execute() 后这里没有错误。

标签: php mysqli


【解决方案1】:

mysqli_stmt_bind_param 接受变量(通过引用)。您不能使用文字。将您的代码更改为

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

【讨论】:

    【解决方案2】:

    请尝试 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();
    

    【讨论】:

    • 这也行得通 - 我不认为在后面有任何区别,但你已经纠正了我在 bind_param 中有一个文字而不是字符串作为参数的问题。
    猜你喜欢
    • 2014-11-25
    • 2016-11-29
    • 2016-02-01
    • 2020-09-23
    • 2010-12-05
    • 2011-06-14
    • 2015-01-10
    • 2016-02-24
    • 2011-01-18
    相关资源
    最近更新 更多