mysqli_stmt_bind_result not returning a value

2019-03-01 02:02发布

问题:

Alright so this bugs the crap out of me, and I can't seem to find anything in the PHP documentation, nor anywhere in the Google resultosphere, so maybe someone can help here.

I'm trying to make a simple request to a database to return a varchar(30).

Code:

$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");
}

As you can guess the output was EMPTY. I have tried using this solution: Strange issue with mysqli_stmt_bind_result but it did not work.

To make sure that everthing was suppost to go correctly I have made the query in the mysql and it worked wonders:

select idPhotos from housesphotos where idHouse = 106

OUTPUT:

591219e92b2fe_591219e92b302

The housephotos is a varchar(30) field. I'm not sure if it's the field type that is messing with the return value. I'm not sure also if it's the connections that I made earlier on the code but I have tried unset($stmt) and other variables to solve the problem but it's not working.

Is this some kind of bug with mysqli_stmt_bind_result() or just a new person's mistake?

回答1:

You're not actually fetching the results - you commented the mysqli_stmt_fetch() out. The fetch also returns a boolean, not the actual results. Uncomment that line, and remove the variable assignment to it.

$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;
        }
    }
}

If you still don't get any results, it might be because the number of rows returned was zero (you can check that with $stmt->num_rows after executing), but since you said the query worked in phpMyAdmin, it's likely that there's some sort of error you're ignoring. To check for errors, add an else to your if conditions, and log $stmt->error inside.

  • http://php.net/manual/en/mysqli-stmt.fetch.php


回答2:

It seems that PHP 5.4 (at least the way it's installed on Comcast servers) has a bug that causes bind-result to fail on a varchar field. Changing the field definition to "text" solves the problem.



标签: php mysqli