2016-09-22 125 views
-3

我需要用2个表格工作,我尝试了内部连接,4个结果的位置,它只是显示了两个表格的结果,我正在粘贴这两个查询,请看它并帮助我,我会感谢你们所有人。PHP循环查询不起作用

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

当我打印/回波

$es_touring_city = $row5['es_touring_city']; 

其示出图4的结果。

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

如果我不是很清楚说话。

谢谢,

+0

每次你在新的使用[了'mysql_'(http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) 数据库扩展代码 ** [小猫被勒死在世界的某个地方](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)**它被弃用,并已多年,并在PHP7中永远消失。 如果您只是学习PHP,请花些精力学习'PDO'或'mysqli'数据库扩展。 [从这里开始](http://php.net/manual/en/book.pdo.php) – RiggsFolly

+0

你不应该使用这样的嵌套查询。他们非常低效。您应该重写为单个“连接”查询。你已经加入了第二个,所以扩展它们以覆盖父/子查询。 –

+1

不要使用'@'错误消音器。如果你有错误,先显示然后第二个修复他们 – RiggsFolly

回答

0

将所有内容加入到单个查询中。对结果进行排序,以便同一个es_id的所有行都在一起。然后检查$row['es_id']何时更改,并开始新行。

<?php 

$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, i.image 
     FROM escorts AS e 
     INNER JOIN escorts_touring AS t ON e.es_id = t.es_id 
     INNER JOIN (SELECT es_id, MAX(image) AS image 
        FROM escorts_image 
        GROUP BY es_id) AS i ON e.id = i.es_id 
     ORDER BY e.es_id"; 
$result = mysql_query($sql); 
$last_esid = null; 
$counter = 0; 

$dest = "uploads"; 

while ($row = mysql_fetch_assoc($result)) { 
    $es_id=$row['es_id']; 
    if ($counter++ == 4 || $es_id != $last_esid) { 
     if ($last_esid) { 
      echo "</div>"; 
     } 
     echo "<div class='row'></div>"; 
     $last_esid = $es_id; 
     $counter = 0; 
    } 

    $es_sex =$row['es_sex']; 
    $service_type=$row['service_type']; 
    $working_name=$row['working_name']; 
    $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']; 
    $image = $row['image']; 

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

    // print the row 
} 
+0

@barmer,非常感谢你,它似乎工作正常,我编辑和它的工作,因为我想,只有一个问题,每个成员上传近20张相同的照片与相同的es_id,第一个图像始终显示在屏幕上,而不是任何图像,所以如果只是那部分引导我,那么一切都会完成! – Arvind

+0

@barmer,再次感谢你我也找到了图像解决方案,非常感谢! – Arvind

+0

您的原始查询随机选择了一张图片。我需要选择一个图像,所以我任意选择了'MAX(图像)'。 – Barmar