MYSQL While with mysql_fetch_assoc not returning when =1
I have the following PHP that creates some JSON from a MySQL query. This works fine when there is more than one element i.e. mysql_fetch_assoc > 1 but if it = 1 then nothing works and debugging, any echo statements inside the while loop do not get called!
What is up with that?
$byFlight = mysql_query($query_byFlight, $pfArchiveDB) or die(mysql_error());
$row_byFlight = mysql_fetch_assoc($byFlight);
//$totalRows_byFlight = mysql_nu开发者_如何学编程m_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));
You are doing the fetch twice, and that consumes your first row perhaps. This code will work fine:
$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));
精彩评论