php - mysqli bind_result 仅适用于 INT 类型的列

标签 php mysql mysqli prepared-statement

我有一个 mySQL 表定义为:

CREATE TABLE IF NOT EXISTS `coupons` (
    `coupon_id` int(11) NOT NULL auto_increment,
    `coupon_code` varchar(50) NOT NULL,
    `coupon_expiration_date` date NOT NULL,
    `coupon_discount` decimal(10,2) NOT NULL,
    `coupon_created_date` date NOT NULL,
    PRIMARY KEY  (`coupon_id`)
)

此表中的一项如下:

INSERT INTO `coupons` (`coupon_id`, `coupon_code`, `coupon_expiration_date`, `coupon_discount`, `coupon_created_date`) VALUES (1, 'test', '2012-11-30', '11.00', '2012-10-22');

当尝试使用以下 PHP 代码查询此表时:

$strSelectCoupons = "SELECT coupon_id FROM coupons";
if($stmtSelectCoupons = $mysqlConn->prepare($strSelectCoupons))
{
    $stmtSelectCoupons->execute();
    $stmtSelectCoupons->bind_result($iCouponId);

    while($stmtSelectCoupons->fetch())
    {
        echo $iCouponId;
    }
}
else
{
    echo "Prepared statement error: " . $mysqlConn->error;
}

正如预期的那样,我们看到屏幕上打印了一个“1”。

但是,如果我们尝试执行以下 PHP 代码:

$strSelectCoupons = "SELECT coupon_id, coupon_code FROM coupons";
if($stmtSelectCoupons = $mysqlConn->prepare($strSelectCoupons))
{
    $stmtSelectCoupons->execute();
    $stmtSelectCoupons->bind_result($iCouponId, $strCouponCode);

    while($stmtSelectCoupons->fetch())
    {
        echo $iCouponId . "<br />";
        echo $strCouponCode . "<br />";
    }
}
else
{
    echo "Prepared statement error: " . $mysqlConn->error;
}

不显示结果,不打印错误,不记录错误,页面的其余部分不呈现。

事实上,只有当返回 ONLY coupon_id 时,这才真正按预期工作。例如,如果我们将 select 语句更改为仅返回 coupon_code,这将再次导致页面崩溃。

关于导致失败的原因有什么想法吗?

Apache 版本:2.2.2

PHP 版本:5.3.14

mySQL 版本:5.0.95-社区

编辑:

在进一步调查中,bind_result 仅适用于 INT 类型的列,并且在尝试绑定(bind)其他类型的任何列时失败。

最佳答案

关于 mysqli_stmt_bind_result() 的评论指向 a bug in the MySQL Client Library这会导致大数据列崩溃。

据报道,mysqlnd 和 MySQL 的 Connector/C 没有出现此问题,或者如果可能,您可以调用 mysqli_stmt_store_result()在调用 mysqli_stmt_bind_result() 之前。

关于php - mysqli bind_result 仅适用于 INT 类型的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13014971/

相关文章:

php - 根据选择更新表,可能使用锁定?

php - 如何将 Google Charts 与 mySQL 数据结合使用?

mysql - 使用 LINQ/lambda .net MVC 从表中选择某些列的所有数据?

mysql - 如何使用 2 个条件将多行从一个表复制到另一个表

php - 将 2 个内部联接结果合并为一个结果

php - 复用 MySQL 连接 PHP 对象继承

php - Laravel 4 中的列歧义错误

php - 后增量表演很奇怪

Mysql 无法正常工作

php - 插入数据库失败