循环出mysql数据 [英] Looping out mysql data

查看:34
本文介绍了循环出mysql数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有日期的条目的 mysql 数据库.所以我想要的是显示我的数据库中的所有日期,然后在每个日期下,我想显示在特定日期输入的数据库中的所有条目.我正在考虑两个循环,但在循环出该日期下的条目之前,我不知道如何编写条件以显示我的数据库中的所有日期.

I have a mysql database of entries with dates. So what I want is to show all the dates in my database and then under each date, I want to show all the entries in the database entered on the specefic date. I am thinking of two loops but I don't know how to write the condition to display all the dates in my database before I loop out the entries under that date.

我使用 NOW() 来存储日期.使用longneck的循环,如何在检查日期是否与以前相同时忽略时间?我应该爆炸吗?最好的方法是什么?

i used NOW() to store the date. Using the loop by longneck, how can ignore the time when checking if date is sameas previous? Should I explode? What's the best way?

推荐答案

你应该使用一个查询,按日期排序,并检测日期的变化作为显示下一个日期的信号:

you should use one query, sort it by date, and detect the change in the date as a signal to display the next date:

<?php

$sql = 'select start_date, name from events order by start_date';

$res = mysql_query($sql) or die(mysql_error());

$prev_date = null;

while ($row = mysql_fetch_assoc($res)) {
  if ($row['start_date'] != $prev_date) {
    echo "<h1>{$row['start_date']}</h1>"\n;
    $prev_date = $row['start_Date'];
  }

  echo "<p>{$row['name']}</p>";
}

?>

这篇关于循环出mysql数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆