dqyq88053 2014-08-04 22:30
浏览 57
已采纳

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-04 22:38
    关注

    I guess this is because you set

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

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

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部