我有以下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));
答案 0 :(得分:3)
你正在进行两次获取,这可能会消耗你的第一行。 这段代码可以正常工作:
$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));