phpprepared-statement

Why would a prepared statement work using get_result but not when binding the results?


I am having to change over from using get_result() to bind_result() and it is causing a lot of issues. With get_result the query works perfectly! I have LITERALLY seen this go from working one second to not working the next with one change to the code, removing echo $joinid and even have one page that the query fails halfway through a while loop and only returns 2 of 4 known results before throwing the bind param error.

And I always get the error:

Fatal error: Call to a member function bind_param() on boolean in

For instance The following when I use get_result()works fine.

$sql = "SELECT id, auction_title, auction_date, auction_time, auction_address, auction_city, auction_state, auction_zipcode, description FROM auctions WHERE join_id = ?";

    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i",$row['join_id']);
    $stmt->execute();
    $result = $stmt->get_result();

Though this fails.

NOTE:$joinid comes from another query and echos out to what it should.

$sql2 = "SELECT id, auction_title, auction_date, auction_time, auction_address, auction_city, auction_state, auction_zipcode, description FROM auctions WHERE join_id = ?";

    $stmt = $conn->prepare($sql2);
    $stmt->bind_param("i",$joinid);
    $stmt->execute();

    $stmt->bind_result($id, $auction_title, $auction_date, $auction_time, $auction_address, $auction_city, $auction_state, $auction_zipcode, $description);

Here is the var_dump with a side by side comparison, different query but same results overall:

$sql = "SELECT join_id, bus_name, first_name, last_name, address, city, state, zip, phone, fax, email, wsaddr, image_name, bio FROM auctioneer WHERE id = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i",$getVar);
$stmt->execute();
//$results = $stmt->get_result();
$stmt->bind_result($join_id, $bus_name, $first_name, $last_name, $address, $city, $state, $zip, $phone, $fax, $email, $wsaddr, $image_name, $bio);
var_dump($stmt);
//$stmt->close();

Using get_result: object(mysqli_stmt)#2 (10) { ["affected_rows"]=> int(1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(1) ["field_count"]=> int(14) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) }

Using bind_result: object(mysqli_stmt)#2 (10) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(1) ["field_count"]=> int(14) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) }


Solution

  • For anyone who might come along looking for it later, in my case the correct answer was adding store_result().

    $sql = "SELECT id, auction_title, auction_date, auction_time, auction_address, auction_city, auction_state, auction_zipcode, description FROM auctions WHERE join_id = ?";
    
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("i",$join_id);
        $stmt->execute();
    
        $stmt->store_result();
        $stmt->bind_result($id, $auction_title, $auction_date, $auction_time, $auction_address, $auction_city, $auction_state, $auction_zipcode, $description);