循环中循环内的SQL查询

时间:2011-02-15 17:37:34

标签: php sql

嘿大家好我试图以30分钟的间隔打印出一个时间表,并希望查询数据库以获取当时正在发生的任何事情。如果我手动输入时间(小时,分钟,上午/下午(月,年,日工作))我得到事件。只是当我让查询从循环中抽出时间时,这不起作用。有任何想法吗?

$day = date('d');
$year = date('Y');
$month = date('m');
$start = mktime(0,0,0);


$thing_query="SELECT thing FROM things WHERE day='$day' AND month='$month' AND year='$year' AND hour='$hour' AND minute='$minute' AND ampm='$am'";


$result=mysql_query($thing_query);

for($min = 0; $min < 24 * 60 * 60; $min += 30 * 60)
{
    $hour=date("h", $start + $min);
    $minute=date("i", $start + $min);
    $am=date("A", $start + $min);

    while ($row=mysql_fetch_array($result)) {
        $thing = $row[0];
}

   printf("<tr><td>%s</td><td>$thing</td></tr>",
          date("g:i a", $start + $min));


} 

4 个答案:

答案 0 :(得分:1)

我想这是因为你在查询数据库之前没有设置小时分钟和安培。

你可能需要在每个循环中使用新的小时,分​​钟等来重新查询数据库,但是可能有一种更有效的方法来执行此操作...即在整天数据中使用db一次,然后使用PHP迭代信息。 1分钟呼叫24 * 60 * 60

下面的代码未经测试,如果它不能完全正常工作,请打电话给我,但它应该给你一个想法:

$day = date('d');
$year = date('Y');
$month = date('m');
$start = mktime(0,0,0);



$thing_query="SELECT thing FROM things WHERE day='$day' AND month='$month' AND year='$year';


$result=mysql_query($thing_query);


    while ($row=mysql_fetch_array($result)) {
        // Loop through your hours mins etc and output desired values
}



} 

您需要的所有数据都存储在mysql_fetch_array($result)中,然后您可以根据需要循环显示。你不想超过必要的数据库。

从我可以看到你有PHP能够做到这一点 - 我认为它是你正在努力的结构。

答案 1 :(得分:1)

您的循环结构错误 - 您在第一次运行父for循环时消耗了整个查询结果集。而你的内部while循环只是将$thing重复设置为一个字段的值,因此$ thing最终成为查询返回的最后一个值。

评论后续。做你想做的低效方法是:

$day = ...
$year = ...
$month = ...
$start = ...

for ($min = 0; ....) {
   $hour = ...
   $min = ...
   $am = ...

   $thing_query = "SELECT ...."
   $result = mysql_query($thing_query) or die(mysql_error());
   while($row = mysql_fetch_array($result)) {
      printf(.... $row[0] ... );
   }
}

但是这会针对您要检查的每个时间点运行查询。为什么不将事件的日期/时间存储在单个日期/时间字段中。你可以减少查询到

SELECT ... WHERE timestampfield BETWEEN startdatetime AND enddatetime

然后使用PHP中的结果来构建您的事件。单个“更大”的查询比一系列“小”查询更有效。

答案 2 :(得分:1)

字符串中的变量引用仅在赋值时有效;如果您更改这些变量,它们不会继续更新,所以当您执行

$thing_query="SELECT thing FROM things WHERE day='$day' AND month='$month' AND year='$year' AND hour='$hour' AND minute='$minute' AND ampm='$am'";

无关
$thing_query="SELECT thing FROM things WHERE day='15' AND month='2' AND year='2011' AND hour='' AND minute='' AND ampm=''";

因为$ hour,$ minute和$ am尚未设置,因此查询不返回任何内容。

即使它们是,更新字符串也不会更新数据库查询;你需要在新字符串上调用mysql_query()来获取该数据。

如果你将$ thing_query =和$ res =行移到while循环之前,这应该可以工作,虽然它只返回每个时间段中的最后一个事件,因为每次你通过时都会覆盖$ thing环。它还将继续在每个时间段列出一个事件,直到它到达一个新的时间段,因为你没有清除$ thing。

正如Andy所说,目前这不是一个非常有效的方式来做你想要的,但是因为你可能刚刚开始我猜你现在更重要的是它有效而不是效率,所以希望现在有所帮助。

答案 3 :(得分:1)

你把陈述的顺序搞得很糟糕。这是正确的方式,还有一些额外的评论:

$day = date('d');
$year = date('Y');
$month = date('m');
$start = mktime(0,0,0);



for($min = 0; $min < 24 * 60 * 60; $min += 30 * 60)
{
    $hour=date("h", $start + $min);
    $minute=date("i", $start + $min);
    $am=date("A", $start + $min);

    // you must set the string after $hour/$minute/$date have the right value
    $thing_query="SELECT thing FROM things WHERE day='$day' AND month='$month' AND year='$year' AND hour='$hour' AND minute='$minute' AND ampm='$am'";

    // query the database with the string
    $result=mysql_query($thing_query);

    // put things in an array
    $things = array();
    while ($row=mysql_fetch_array($result)) {
        $things[] = $row[0];
    }

   // join the array so I have a comma separated list of things
   $listOfThings = implode(", ", $things);

   // ALWAYS use htmlspecialchars when sending data from the database to the browser!!!!
   echo "<tr><td>" . date("g:i a", $start + $min) . "</td><td>" . htmlspecialchars($listOfThings) . "</td></tr>";


}