douye9822 2010-12-23 14:19
浏览 65
已采纳

php json_encode错过了第一条记录

i am retriving a .php file which json_encodes the results from a recordset.

it produces the json fine but it misses the first record in the recordset,

so if i have 21 rows it only creates a json object with 20 records, with the first one missing.

function recordSetToJson($mysql_result) {
 $rs = array();
 while($rs[] = mysql_fetch_assoc($mysql_result)) {
    // you don´t really need to do anything here.
  }
 return json_encode($rs);
}
echo '{"myFiles":'.recordSetToJson($UserFiles).'}';

i tried:

function recordSetToJson($mysql_result) {
 $rs = array();
 do{
    // you don´t really need to do anything here.
  }
 return json_encode($rs);
}while($rs[] = mysql_fetch_assoc($mysql_result)) 
echo '{"myFiles":'.recordSetToJson($UserFiles).'}';

but this just produces a blank page.

  • 写回答

2条回答 默认 最新

  • dqspy04266 2010-12-23 14:33
    关注

    simple solution!!!

    i added mysql_data_seek($mysql_result, 0); to the function to reset the recordset back to the first row, don't know why it was forwared by 1 record in the firstplace but, it works now.

    the full function:

    <?php 
        function recordSetToJson($mysql_result) {
            $rs = array();
            mysql_data_seek($mysql_result, 0);
            while($rs[] = mysql_fetch_assoc($mysql_result)) {
                // dont need anything here!!
            }
            return json_encode($rs);
        };
    echo '{"myFiles":'.recordSetToJson($UserFiles).'}';
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b