我在 PHP 中有这段代码,我试图根据准备好的语句的结果制作一个 JSON。问题是它返回一个完全白色的页面,什么也没有出现。
$con = mysqli_connect(HOST,USER,PASS,DB);
$batch_sql = "select * from batchrecord";
$batch_res = mysqli_query($con,$batch_sql);
$row = mysqli_fetch_array($batch_res);
$batch_num = $row[0];
$start = $batch_num * 100;
$end = $start + 99;
if ($stmt = mysqli_prepare($con, "select tweetid, body from tweet where id >=
? and id <= ?")) {
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "ii", $start, $end);
/* execute query */
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $tweetid, $body);
$result = array();
/* fetch value */
while(mysqli_stmt_fetch($stmt)){
array_push($result,
array('Id'=>$tweetid,
'Body'=>$body,
));
}
/* close statement */
mysqli_stmt_close($stmt);
echo json_encode(array("result"=>$result), JSON_UNESCAPED_UNICODE);
}
else{
echo "Statement Prepare Error";
}
mysqli_close($con);
我已经将 $tweetid 和 $body 的内容作为测试打印在 while 语句中,并且工作正常,这意味着问题不在于查询,而在于数组。我错过了什么?
谢谢!
最佳答案
这样试试
$result = array();
while(mysqli_stmt_fetch($stmt)){
$result[] = array('Id'=>$tweetid,'Body'=>$body);
}
关于php - 如何从 PHP 中的 MySQL 准备语句生成 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45891478/