PHP循环查询不起作用

时间:2016-09-22 20:53:19

标签: php mysql

我需要使用2个表,我尝试使用内部联接,4个结果的位置,它只显示两个表的结果,我粘贴两个查询,请看它并帮助我,我将感谢所有人。

<? 
    $query5 = "SELECT * FROM escorts_touring order by es_tou_id";
    $result5 = mysql_query($query5);
    while($row5 = @mysql_fetch_array ($result5, MYSQL_ASSOC)) 
    {
        $es_touring_city = $row5['es_touring_city'];
    }

?>

<?php 

        echo $sql="SELECT e.es_id, e.es_sex, e.service_type, 
                          e.working_name,  t.es_tou_id, t.es_id, 
                          t.es_touring_city, t.es_touring_start_date, 
                          t.es_touring_end_date 
                    FROM escorts AS e 
                        INNER JOIN escorts_touring AS t 
                            ON e.es_id = t.es_id 
                    where es_touring_city = '$es_touring_city'";
        $result=mysql_query($sql); 
        $rowcount=mysql_num_rows($result);
        $counter=0;
        $count=0;

        while($row = @mysql_fetch_array ($result, MYSQL_ASSOC)) 
        {

            if($counter++%4==0)print"</div><div class=\"row\"></div>";

            $es_sex  =$row['es_sex'];
            $service_type=$row['service_type'];
            $working_name=$row['working_name'];
            $es_id=$row['es_id'];
            $es_tou_id = $row['es_tou_id'];
            $es_touring_city = $row['es_touring_city'];
            $es_touring_start_date = $row['es_touring_start_date'];
            $es_touring_end_date =$row['es_touring_end_date'];

            $newstartDate = date("dS F, Y", strtotime($es_touring_start_date));
            $newendDate = date("dS F, Y", strtotime($es_touring_end_date));

            $query = "SELECT * FROM escorts_image where es_id = $es_id";
            $result_image = @mysql_query ($query);
            $row_image = @mysql_fetch_array ($result_image, MYSQL_ASSOC);
            $image = $row_image['image'];
            $dest="uploads";
        ?>

当我打印/ echo

$es_touring_city = $row5['es_touring_city'];

显示4结果。

但是当我在第二个查询中使用$ es_touring_city时,它只显示带有图像的2个结果。

如果我不是很清楚说话。

谢谢,

1 个答案:

答案 0 :(得分:0)

将所有内容加入到单个查询中。对结果进行排序,以便同一+----------------------+ | result | +----------------------+ | ["ROOT1","N1","SN2"] | | ["ROOT1","N2","SN3"] | +----------------------+ 的所有行都在一起。然后检查es_id何时更改,并开始新行。

$row['es_id']