2016-09-22 59 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