gpt4 book ai didi

php - MYSQL 当 mysql_fetch_assoc =1 时不返回

转载 作者:行者123 更新时间:2023-11-29 06:17:56 26 4
gpt4 key购买 nike

我有以下 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 当 mysql_fetch_assoc =1 时不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5429633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com