douweida2669 2013-03-26 13:34
浏览 19
已采纳

For循环不返回所有值

I'm working on a piece of code that's meant to be implemented in Google Charts, however, not all values I'm looking for are being returned.

I need a couple of dates selected from my database, and I select them as follows:

$dates = mysql_fetch_row(mysql_query("SELECT DISTINCT date FROM participants WHERE subdomein='testdomain'")) or die(mysql_error());

Then I use a for-loop to echo them:

for ($i = 0; $i <= count($dates); $i++)
    {
        echo $dates[0].' ';
    }

In my database there are 3 (distinct) dates: 24-03-2013, 25-03-2013 and 26-03-2013, however the piece of code returns 2x 24-03-2013. What am I doing wrong here?

P.S. I also tried a while-loop but that loops infinitely or crashes my page. Besides that, I tested the query by running it in the database itself and it returns the right results, so the query works fine.

Help is much appreciated!

  • 写回答

3条回答 默认 最新

  • douchi7073 2013-03-26 13:38
    关注

    You need to use mysql_fetch_row() inside a loop in itself:

    $result = mysql_query("SELECT DISTINCT date FROM participants WHERE subdomein='testdomain'");
    while($date = mysql_fetch_row($result))
    {
        echo $date[0];
    }
    

    You should also note that the mysql_* family of functions are not deprecated. You should avoid using them if possible, and look into alternatives such as MySQLi or PDO.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问异常处理之后怎样继续后面的程序呀?
  • ¥15 懂Matlab的人,可以帮我看看第四题和第五题应该怎么才能运行出来,或者可以提供一点相关的思路
  • ¥15 cv2.fisheye.stereoCalibrate报错
  • ¥20 求自动化运维语料数据集
  • ¥30 广告检测流量作弊案例 IDEA运行代码报错 连接不上metastore 检测了环境配置没有问题 请求远程解决加VX问细节问题 不加的不回复
  • ¥15 matlab图像融合代码被嫌弃太简单,求改进。第一步改成直接读取三张图片,不读取文件夹
  • ¥20 微处理器原理与应用(私有偿)
  • ¥50 8051单片机关于ADC0809的应用
  • ¥15 有没有能拿来练练手写完发给我
  • ¥15 禁止修改windows系统时间