2011-12-12 101 views
0

問題標題不是很清楚,所以讓我解釋一下。如何在一個循環內的php mysql中查詢結果的子查詢

我試圖使該得到的所有行項目爲一個特定的順序,所以我使用一個循環來獲取所有訂單的行項目表不過我行項目表結構如下

|Id (pk)| Order_Id (fk) | Line_Id | Item_Id (fk) | Item_Qty | Item_Price | Line_Total| 
|1  | 1    | 1  | 1   | 1  | 25   |25   | 
|2  | 1    | 2  | 2   | 3  | 6   |18   | 

我現在想在這個循環中進行查詢,從items表中獲取item_number,這樣它不僅可以顯示有點模糊的item_id。

我的主要問題是我似乎無法弄清楚如何讓查詢知道在SQL語句的WHERE子句中使用哪個item_id。以及如何讓循環內部每次運行父循環/查詢時都不運行,即3行項目,因此子查詢/循環將運行3次而不是一次,因爲有3個值在它正在循環的數組中。

$id = $_GET['id']; 

$query = "SELECT * "; 
$query.= "FROM quotes "; 
$query.= "WHERE id = {$id}"; 

$confirmed_query = confirm_query($query); 
    if (!$confirmed_query) { 
     echo "Query Failed".mysql_error(); 
     } 

$query = mysql_query($confirmed_query); 

$query_result = mysql_fetch_assoc($query); 

echo "<table border=\"1\"><tr>"; 

foreach ($query_result as $key => $value) { 
    echo "<th>".ucwords(str_replace("_", " ", $key))."</th>"; 
    } 

echo "</tr><tr>"; 

foreach ($query_result as $key => $value) { 
    echo "<td>{$value}</td>"; 
    } 

echo "</tr></table><br /><br /><table border=\"1\"<tr>"; 

$query = "SELECT * "; 
$query.= "FROM quote_details "; 
$query.= "WHERE quote_id = {$id}"; 

$confirmed_query = confirm_query($query); 
    if (!$confirmed_query) { 
     echo "Query Failed".mysql_error(); 
     } 

$query = mysql_query($confirmed_query); 
$header_written = false; 
echo "</tr><tr>"; 
while ($query_result = mysql_fetch_assoc($query)) { 
    while ($header_written == false) { 
     foreach ($query_result as $key => $value) { 
      echo "<th>".ucwords(str_replace("_", " ", $key))."</th>"; 
      $header_written = true; 
      } 
     } 
    echo "</tr><tr>"; 

    //problem starts here 

    foreach ($query_result as $key => $value) { 

     $item_query = "SELECT item_number "; 
     $item_query.= "FROM items "; 
     $key_item = $key; 
     $item_query.= "WHERE id = {$key_item}"; 

     $confirmed_query = confirm_query($item_query); 
      if (!$confirmed_query) { 
       echo "Query Failed".mysql_error(); 
       } 

     $item_query = mysql_query($confirmed_query); 
     while ($item_number = mysql_fetch_assoc($item_query)) { 
      foreach ($item_number as $item) { 
       echo $item; 
      } 
     } 

     echo "<td>{$value}</td>"; 
     } 
    } 
echo "</tr></table>"; 

請不要介意顯示html與sql查詢混在一起。

任何關於如何解決這個問題的幫助將會很棒我似乎無法找到一個角度來開始工作。

謝謝。

+0

好吧我已經取得了一些進展,通過更改SQL語句使用它現在正在做的$值。但是我仍然無法從多次輸出結果中獲得它。 另外我怎樣才能夠item_id替換item_id的「1」,它是拉回來? – Moshe

+0

附註:您有主要的SQL注入漏洞 – Petah

+0

另外,學習使用'JOIN'語句:'SELECT * FROM引用AS q JOIN quote_details AS qd ON qd.quote_id = q.id JOIN items AS i ON i.id = qd.item_id' – Petah

回答

1

而不是做一個循環,使用SQL JOIN

你需要像這樣

SELECT * 
FROM quotes AS q 
JOIN quote_details AS qd ON qd.quote_id = q.id 
JOIN items AS i ON i.id = qd.item_id 
0

想通了這一個自己出去只是一堆試驗和錯誤

$item_got = false; 
$id = $_GET['id']; 

$query = "SELECT * "; 
$query.= "FROM quotes "; 
$query.= "WHERE id = {$id}"; 

$confirmed_query = confirm_query($query); 
if (!$confirmed_query) { 
    echo "Query Failed".mysql_error(); 
} 

$query = mysql_query($confirmed_query); 

$query_result = mysql_fetch_assoc($query); 

echo "<table border=\"1\"><tr>"; 

foreach ($query_result as $key => $value) { 
    echo "<th>".ucwords(str_replace("_", " ", $key))."</th>"; 
} 

echo "</tr><tr>"; 

foreach ($query_result as $key => $value) { 
    echo "<td>{$value}</td>"; 
} 

echo "</tr></table><br /><br /><table border=\"1\"><tr>"; 

$query = "SELECT * "; 
$query.= "FROM quote_details "; 
$query.= "WHERE quote_id = {$id}"; 

$confirmed_query = confirm_query($query); 
if (!$confirmed_query) { 
    echo "Query Failed".mysql_error(); 
} 

$query = mysql_query($confirmed_query); 
$header_written = false; 
echo "</tr><tr>"; 
while ($query_result = mysql_fetch_assoc($query)) { 
    while ($header_written == false) { 
     foreach ($query_result as $key => $value) { 
      echo "<th>".ucwords(str_replace("_", " ", $key))."</th>"; 
      $header_written = true; 
     } 
    } 
    echo "</tr>"; 
    echo "<tr>"; 
    //problem starts here 

    foreach ($query_result as $key => $value) { 

     if ($key == "item_id"){ 
      $item_query = "SELECT item_number "; 
      $item_query.= "FROM items "; 
      $key_item = $value; 
      $item_query.= "WHERE id = {$key_item}"; 


      $confirmed_query = confirm_query($item_query); 
      if (!$confirmed_query) { 
       echo "Query Failed".mysql_error(); 
      } 

      $item_query = mysql_query($confirmed_query); 
      while ($item_number = mysql_fetch_assoc($item_query)) { 

       foreach ($item_number as $item) { 
        echo "<td>{$item}</td>"; 
       } 
      } 
     } 
     elseif ($key == "quote_id"){  
      $item_query = "SELECT quote_number "; 
      $item_query.= "FROM quotes "; 
      $key_quote = $value; 
      $item_query.= "WHERE id = {$key_quote}"; 

      $confirmed_query = confirm_query($item_query); 
      if (!$confirmed_query) { 
       echo "Query Failed".mysql_error(); 
      } 

      $item_query = mysql_query($confirmed_query); 
      while ($item_number = mysql_fetch_assoc($item_query)) { 

       foreach ($item_number as $item) { 
        echo "<td>{$item}</td>"; 
       } 
      } 
     } 

     else { 
      echo "<td>{$value}</td>"; 
     } 
    } 
    echo "</tr>"; 
} 
echo "</table>;" 
相關問題