dqyq88053 2014-08-05 06:30
浏览 56
已采纳

mysql_fetch_array只运行一次,应该是六次

I can see in my logcat that the $check query is only running once, when it should be running once for every result of the $result query, which is 6. I've been troubleshooting this for hours and I just can't figure out why it's not working properly.

Is there an obvious reason why $check is only running once?

$result = mysql_query("SELECT * FROM `posts` WHERE facebook_id = $fbid ORDER BY id DESC") or die(mysql_error());
while ($row1 = mysql_fetch_array($result)) 
{

    $mytime = $row1['time'];
    $mylat = $row1['latitude'];
    $mylon = $row1['longitude'];
    $mypostid = $row1['id'];

    // get all products from products table
    $check = mysql_query("SELECT facebook_id as fid, id as uid,
        TIMESTAMPDIFF(SECOND, `time`, '$mytime') AS timediff
        FROM `posts`
        WHERE `facebook_id` != $fbid
        HAVING `timediff` <= '180'
        ORDER BY `time` DESC") or die(mysql_error());
    if (mysql_num_rows($check) > 0) 
    {
        $response["products"] = array();
        while ($row = mysql_fetch_array($check)) 
        {
            // temp user array
            $product = array();
            $product["facebookid"] = $row["fid"];
            $product["timediff"] = $row["timediff"];
            $product["theirpostid"] = $row["uid"];
            $product["mypostid"] = $mypostid;

            // push single product into final response array
            array_push($response["products"], $product);
        }
    }
}
$response["success"] = 1;
echo json_encode($response);
  • 写回答

1条回答 默认 最新

  • dqr91899 2014-08-05 06:38
    关注

    I guess this is because you set

    $response["products"] = array();

    inside every cycle. Move it out of this inner while.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看