为什么准备好的语句使用get_result而不是在绑定结果时工作?

时间:2015-11-30 17:37:33

标签: php prepared-statement

我不得不将使用get_result()转换为bind_result(),这会导致很多问题。使用get_result,查询工作完美!我已经看到这从一秒钟工作到下一次没有工作,只需对代码进行一次更改,删除echo $joinid甚至有一个页面,查询在while循环中途失败,只返回4个已知结果中的2个在抛出绑定参数错误之前。

我总是得到错误:

  

致命错误:在

中的布尔值上调用成员函数bind_param()

例如,当我使用get_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",$row['join_id']);
    $stmt->execute();
    $result = $stmt->get_result();

虽然失败了。

注意:$ joinid来自另一个查询并回应它应该是什么。

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

这是var_dump并排比较,不同的查询但整体结果相同:

$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();

使用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) }

使用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) }

2 个答案:

答案 0 :(得分:1)

对于可能会在以后寻找的人,在我的情况下,正确的答案是添加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);

答案 1 :(得分:0)

mysqli_stmt::bind_result只是告诉mysqli扩展名在从查询中检索数据时放置值的位置,它实际上不会从结果集中获取下一行,您必须使用$stmt->fetch()

执行此操作

所以

$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);
$stmt->fetch();   // this actually fills the bound variables
echo $id;  // etc