mysqli_stmt_bind_result没有返回值

时间:2017-05-09 21:13:41

标签: php mysqli

好吧所以这会让我的垃圾漏掉,我似乎无法在PHP文档中找到任何内容,也无法在Google结果中找到任何内容,所以也许有人可以在这里提供帮助。

我试图向数据库发出简单请求以返回varchar(30)

代码:

$qryID = "select idPhotos from housesphotos where idHouse = ?";
//Prepare the query
$stmt = mysqli_prepare($link, $qryID);

$x = 106;
 //Bind the login parameter to the statement
if(mysqli_stmt_bind_param($stmt, "i", $x)){
    if(mysqli_stmt_execute($stmt)){
        //Bind every column in the select
        if(mysqli_stmt_bind_result($stmt, $photoID)){
//                $photoID = mysqli_stmt_fetch($stmt);
            echo $photoID;
        }
    }
}

if(empty($photoID)){
    printf("EMPTY");
}

您可以猜测输出为EMPTY。我尝试过使用此解决方案:Strange issue with mysqli_stmt_bind_result但它没有用。

为了确保everthing是正确的,我已经在mysql中进行了查询,并且它创造了奇迹:

select idPhotos from housesphotos where idHouse = 106

输出:

591219e92b2fe_591219e92b302

housephotosvarchar(30)字段。我不确定它是否是正在弄乱返回值的字段类型。我不确定它是否是我之前在代码上建立的连接,但我已尝试unset($stmt)和其他变量来解决问题,但它无法正常工作。

这是mysqli_stmt_bind_result()的某种错误还是只是一个新人的错误?

2 个答案:

答案 0 :(得分:2)

您实际上并未提取结果 - 您已将mysqli_stmt_fetch()注释掉了。 fetch还返回一个布尔值,而不是实际结果。取消注释该行,并删除对其的变量赋值。

$qryID = "select idPhotos from housesphotos where idHouse = ?";
//Prepare the query
$stmt = mysqli_prepare($link, $qryID);

$x = 106;
 //Bind the login parameter to the statement
if(mysqli_stmt_bind_param($stmt, "i", $x)){
    if(mysqli_stmt_execute($stmt)){
        //Bind every column in the select
        if(mysqli_stmt_bind_result($stmt, $photoID)){
            mysqli_stmt_fetch($stmt); // Remove the comment and variable assignment
            echo $photoID;
        }
    }
}

如果你仍然没有得到任何结果,可能是因为返回的行数为零(你可以在执行后用$stmt->num_rows检查),但是因为你说查询在phpMyAdmin中工作了,你可能会忽略某些错误。要检查错误,请在else条件中添加if,然后在其中记录$stmt->error

答案 1 :(得分:1)

似乎PHP 5.4(至少是在Comcast服务器上安装的方式)存在一个错误,该错误导致在varchar字段上绑定结果失败。将字段定义更改为“文本”即可解决问题。

相关问题