如何为每个语句单独使用两个MySQLi查询?

时间:2013-07-29 21:06:04

标签: php mysqli

我正在尝试在辅助查询中使用MySQLi结果中的一行。 但我得到了一些意想不到的结果。

<?php 

$mysqli = new mysqli('connection');
if ($mysqli->connect_error) {die('Connect Error: ' . $mysqli->connect_error);}

$today = date("Ymd");

$query = "SELECT course FROM dailytips WHERE date = 20130724 GROUP BY course"; 
$result = $mysqli->query($query);
while($row = $result->fetch_array()) 
{
    $rows[] = $row; } 
    foreach($rows as $row) 
    { 
        echo $row['course'] . "<br/>"; 
        $query2 = "SELECT horse, time, date FROM dailytips WHERE date = 20130724 and course ='{$row['course']}' ORDER BY time"; 
        $result2 = $mysqli->query($query2);

        $today_uk = " " . date("d/m/y");
        while($row2 = $result2->fetch_array()) 
        {
            $rows2[] = $row2;
        }
        foreach($rows2 as $row2)
        {
           $date = $row2['date'];
           $date = date("d/m/y", strtotime($date));

           echo '<div style= "width:600px; font-family:verdana;"><div style="float:left; width:400px; margin-bottom:10px; margin-top10px;">'.$row2['time'] . "-" . $row2['horse'] .' </div>' ;                         
        }
    }

$result->close();
$mysqli->close(); 

?>

我的页面目前看起来像 -

ipswich
11:00-running
12:00-flamingo rider
14:00-lightning                  

norwich
11:00-running
12:00-flamingo rider
14:00-lightning
13:10-ed is back
14:05-redrum
17:05-pickle

而我想要

ipswich
11:00-running
12:00-flamingo rider
14:00-lightning 

norwich
13:10-ed is back
14:05-redrum
17:05-pickle

将被退回。

如何在每个查询的第二个结果中释放结果?

1 个答案:

答案 0 :(得分:0)

天啊,好一塌糊涂。 试试这个。

<?php 

$mysqli = new mysqli('connection');
if ($mysqli->connect_error) {die('Connect Error: ' . $mysqli->connect_error);}

$today = date("Ymd");

$query = "SELECT course FROM dailytips WHERE date = 20130724 GROUP BY course"; 
$result = $mysqli->query($query);

$row = $result->fetch_array();

echo $row['course'] . "<br/>"; 
do {

    $query2 = "SELECT horse, time, date FROM dailytips WHERE date = 20130724 and course ='{$row['course']}' ORDER BY time"; 
    $result2 = $mysqli->query($query2);

    $today_uk = " " . date("d/m/y");

    while($row2 = $result2->fetch_array()) 
    { 
        $date = $row2['date'];
        $date = date("d/m/y", strtotime($date));

        echo '<div style= "width:600px;font-family:verdana;"><div style="float:left; width:400px; margin-bottom:10px; margin-top:10px;">'.$row2['time'] . "-" . $row2['horse'] .' </div>' ;                         
    } 

  } while ( $row = $result->fetch_array() );

   $result->close();
   $result2->close();
   $mysqli->close(); 
?>