为什么准备好的语句可以使用 get_result 而不是绑定结果?
Why would a prepared statement work using get_result but not when binding the results?
我不得不从使用 get_result() 改为使用 bind_result(),这导致了很多问题。使用 get_result 查询完美!我真的看到这从一秒钟工作到下一秒钟不工作,对代码进行了一次更改,删除了 echo $joinid
甚至有一页查询在 while 循环中途失败并且只有 returns 2抛出绑定参数错误之前的 4 个已知结果。
而且我总是得到错误:
Fatal error: Call to a member function bind_param() on boolean in
例如,当我使用 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) }
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
对于以后可能会一起寻找它的任何人,在我的情况下,正确的答案是添加 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);
我不得不从使用 get_result() 改为使用 bind_result(),这导致了很多问题。使用 get_result 查询完美!我真的看到这从一秒钟工作到下一秒钟不工作,对代码进行了一次更改,删除了 echo $joinid
甚至有一页查询在 while 循环中途失败并且只有 returns 2抛出绑定参数错误之前的 4 个已知结果。
而且我总是得到错误:
Fatal error: Call to a member function bind_param() on boolean in
例如,当我使用 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) }
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
对于以后可能会一起寻找它的任何人,在我的情况下,正确的答案是添加 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);