我有以下PHP从MySQL查询创建一些JSON。当有多个元素(即mysql_fetch_assoc> 1)时,这很好用,但是如果它= 1,则没有任何效果和调试,while循环内的任何echo语句都不会被调用!
这是怎么回事?
$byFlight = mysql_query($query_byFlight, $pfArchiveDB) or die(mysql_error());
$row_byFlight = mysql_fetch_assoc($byFlight);
//$totalRows_byFlight = mysql_num_rows($byFlight);
//echo ($query_byFlight);
$flights = array();
if(mysql_num_rows($byFlight)) {
while($flight = mysql_fetch_assoc($byFlight)) {
$flights[] = array('flight'=>$flight);
}
}
header('Content-type: application/json');
echo json_encode(array('flights'=>$flights));
最佳答案
您进行了两次提取,可能会占用第一行。
这段代码可以正常工作:
$byFlight = mysql_query($query_byFlight, $pfArchiveDB) or die(mysql_error());
//$row_byFlight = mysql_fetch_assoc($byFlight); // <---- COMMENTED THIS FETCH
//$totalRows_byFlight = mysql_num_rows($byFlight);
//echo ($query_byFlight);
$flights = array();
if(mysql_num_rows($byFlight)) {
while($flight = mysql_fetch_assoc($byFlight)) {
$flights[] = array('flight'=>$flight);
}
}
header('Content-type: application/json');
echo json_encode(array('flights'=>$flights));
关于php - 当mysql_fetch_assoc = 1时不返回MYSQL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5429633/