mysqli select不返回变量

时间:2014-05-14 13:45:52

标签: php sql mysqli

我正在尝试使用mysqli进行简单的选择查询,但似乎它不起作用。有谁看到这里有什么问题?我希望在文件的末尾打印出两个变量。

$stmt = $mysqli->prepare("select billnumber, amount from bill where billid = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($billnumber, $amount);
$stmt->fetch();

var_dump($mysqli);
echo "<br><br>";

var_dump($stmt);
echo "<br>";

echo $billnumber;
echo $amount;

$stmt->close();

输出:

object(mysqli)#2 (19) { ["affected_rows"]=> int(-1) ["client_info"]=> string(79) "mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $" ["client_version"]=> int(50011) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(2) ["host_info"]=> string(25) "Localhost via UNIX socket" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(6) "5.5.28" ["server_version"]=> int(50528) ["stat"]=> string(152) "Uptime: 2619401 Threads: 11 Questions: 220513466 Slow queries: 146 Opens: 14454832 Flush tables: 1 Open tables: 64 Queries per second avg: 84.184" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(3244658) ["warning_count"]=> int(0) } 

object(mysqli_stmt)#4 (10) { ["affected_rows"]=> NULL ["insert_id"]=> NULL ["num_rows"]=> NULL ["param_count"]=> NULL ["field_count"]=> NULL ["errno"]=> NULL ["error"]=> NULL ["error_list"]=> NULL ["sqlstate"]=> NULL ["id"]=> NULL } 

2 个答案:

答案 0 :(得分:1)

mysqli_stmt_close()

  

关闭准备好的声明。 mysqli_stmt_close()也解除了   声明句柄。如果当前语句有待处理或未读   结果,此函数取消它们,以便下一个查询可以   执行。

在阅读结果之前不应该关闭

答案 1 :(得分:0)

试试这个:

$stmt = $mysqli->prepare("select billnumber, amount from bill where billid = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($billnumber, $amount);
while($stmt -> fetch()){

var_dump($billnumber);
echo "<br><br>";

var_dump($amount);
echo "<br>";

echo $billnumber;
echo $amount;
}
$stmt->close();

让我知道它是否有效:)