2011-02-15 171 views
0

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

$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)); 


} 
+0

任何错误?查询是否失败? – 2011-02-15 17:42:30

+0

为什么不在数据库中查询某一天发生的所有事情,通过每隔30分钟的时间间隔进行一次迭代,以及当时是否有事件显示它。这将消除不断发送每个间隔的查询。 – 2011-02-15 17:44:43

+0

顺便说一句:永远不要把变量放在printf字符串中。该变量可能包含'%'字符,并且printf可能会失败。 – 2011-02-15 18:02:21

回答

1

我想象这是因为在查询数据库之前,您没有设置小时分钟和ampm。

你可能需要在每个循环中用新的小时,分​​钟等等循环查询数据库,但是可能有更高效的方法来做到这一点......即打一天数据库的数据,然后使用PHP来迭代信息。 1分贝通话insted的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能够做到这一点 - 我认为它是一个结构的事情,你正在努力。

+0

查询返回undefined $的东西,因为$ hour $ minute和$ am是空白的。我不认为我有足够的PHP技能来知道如何存储数据,进程,并用for循环的正确时间进行回显? – user618273 2011-02-15 17:47:51

+0

我已经通过上面的回答更新了大致说明了我在说什么。 – diagonalbatman 2011-02-15 18:07:36

1

您的循环结构是错误的 - 您正在使用围绕父for循环的第一次运行中的整个查询结果集。而您的内部while循环只是简单地将$thing设置为一个字段的值,所以$ thing最终成为查询返回的LAST值。

评论后续。做你想做会是怎样的一种低效的方法:

$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] ...); 
    } 
} 

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

SELECT ... WHERE timestampfield BETWEEN startdatetime AND enddatetime 

然后使用PHP中的结果来建立你的事件。一个“较大”的查询比一小串“小”查询更有效率。

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'"; 

它equivilent到

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

因为$小时,$分钟,我还没有设置$,和因此查询不返回任何内容。

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

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

正如Andy所说,目前这不是一种非常有效的方法来做你想做的事情,但由于你大概刚刚开始,我猜它对你来说更重要,因为它现在可以工作而不是高效,所以希望这有助于现在。

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>"; 


}