2017-04-30 104 views
2

我正在嘗試爲下拉選擇表單創建選項組,但我在$innerRow = mysqli_fetch_array($innerResults)行上收到以下錯誤。從MySQL結果中下拉選項分組

警告:mysqli_fetch_array()預計參數1被mysqli_result, 布爾給予

代碼

$options = ""; 
$query = "SELECT DISTINCT manufacturer_name 
      FROM product p 
      JOIN manufacturer m 
      ON p.manufacturer_id = m.manufacturer_id"; 
$outerResults = mysqli_query($con, $query); 
while ($outerRow = mysqli_fetch_array($outerResults)) { 
    $options .= "<optgroup label='{$outerRow["manufacturer_name"]}'>"; 

    $query = "SELECT product_id, model_number 
       FROM product 
       WHERE manufacturer_id 
       IN (SELECT manufacturer_id 
       FROM manufacturer 
       WHERE manufacturer_name = {$outerRow["manufacturer_name"]}"; 
    $innerResults = mysqli_query($con, $query); 
    while ($innerRow = mysqli_fetch_array($innerResults)) { 
     $options .= "<option value='{$innerRow["product_id"]}'>{$innerRow["model_number"]}</option>"; 
    } 
    mysqli_free_result($innerOptions); 
} 
mysqli_free_result($outerOptions); 
mysqli_close($con) 
+0

你可以在'$ outerResults ='和'$ innerResults ='語句之前打印$ query;'? – Manngo

回答

0

首先,你內心的疑問是失敗的原因是因爲你還沒有包裹您的{$outerRow["manufacturer_name"]}用單引號作爲字符串值要求,並且您沒有關閉子查詢的括號:'{$outerRow["manufacturer_name"]}');但我不希望您修復您的查詢,請繼續閱讀...

有幾件事情,我想糾正你的過程:

  • 你的第一個查詢不使用的MySQL真正的力量。您真的不應該爲數據庫中的每個製造商提供新的查詢 - 效率不高。
  • 我建議您聲明和條件檢查您的查詢的結果集,以避免WARNING消息並適當地處理錯誤的查詢。
  • 您在構建dom期間未關閉您的<optgroup>標籤。您的輸出將仍然按照需要進行渲染,但標籤旨在關閉。
  • 因爲您沒有使用結果集中的數字鍵,所以應該使用mysqli_fetch_assoc()而不是mysqli_fetch_array()

這是你的代碼完全重寫,這將使得上述更正和完善您的查詢過程爲單一的呼叫:

require_once("db.php"); 
$sql="SELECT M.manufacturer_name,P.product_id,P.model_number 
     FROM manufacturer M 
     LEFT JOIN product P ON M.manufacturer_id=P.manufacturer_id 
     GROUP BY M.manufacturer_id,P.product_id 
     ORDER BY M.manufacturer_name,P.model_number;";  
if($result=mysqli_query($con,$sql)){ 
    if(mysqli_num_rows($result)){ 
     $last_group=null; 
     $select="<select name=\"products\">"; 
      while($row=mysqli_fetch_assoc($result)){ 
       if($row["manufacturer_name"]!=$last_group){ 
        $select.=($last_group!=null?"</optgroup>":"")."<optgroup label=\"{$row["manufacturer_name"]}\">"; 
        $last_group=$row["manufacturer_name"]; 
       } 
       if($row["product_id"]==null){ 
        $select.="<option disabled>No Products</option>"; 
       }else{ 
        $select.="<option value=\"{$row["product_id"]}\">{$row["model_number"]}</option>"; 
       } 
      } 
     $select.="</optgroup></select>"; 
     echo $select; 
     mysqli_free_result($result); 
    }else{ 
     echo "Empty Resultset From Query"; 
    } 
}else{ 
    echo mysqli_error($con);  
} 

我使用這些表結構及數據測試上面的代碼:

CREATE TABLE `product` (
    `product_id` int(10) NOT NULL, 
    `manufacturer_id` varchar(20) NOT NULL, 
    `model_number` varchar(20) NOT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

INSERT INTO `product` (`product_id`, `manufacturer_id`, `model_number`) VALUES 
(1, '1', '#0001'), 
(2, '1', '#0002'), 
(3, '1', '#0003'), 
(4, '2', '#0001'), 
(5, '2', '#0002'), 
(6, '4', '#0001'); 

ALTER TABLE `product` 
    ADD PRIMARY KEY (`product_id`); 

ALTER TABLE `product` 
    MODIFY `product_id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7; 

CREATE TABLE `manufacturer` (
    `manufacturer_id` int(10) NOT NULL, 
    `manufacturer_name` varchar(20) NOT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

INSERT INTO `manufacturer` (`manufacturer_id`, `manufacturer_name`) VALUES 
(1, 'AAAA'), 
(2, 'BBBB'), 
(3, 'CCCC'), 
(4, 'DDDD'); 

ALTER TABLE `manufacturer` 
    ADD PRIMARY KEY (`manufacturer_id`); 

ALTER TABLE `manufacturer` 
    MODIFY `manufacturer_id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5; 

我的代碼塊將輸出這樣的:

<select name="products"> 
    <optgroup label="AAAA"> 
     <option value="1">#0001</option> 
     <option value="2">#0002</option> 
     <option value="3">#0003</option> 
    </optgroup> 
    <optgroup label="BBBB"> 
     <option value="4">#0001</option> 
     <option value="5">#0002</option> 
    </optgroup> 
    <optgroup label="CCCC"> 
     <option disabled>No Products</option> 
    </optgroup> 
    <optgroup label="DDDD"> 
     <option value="6">#0001</option> 
    </optgroup> 
</select> 

這使得爲:

enter image description here

附:如果您不希望結果集中的無產品製造商(或選擇選項列表),請在GROUP BYORDER BY行/條之間寫HAVING P.product_id IS NOT NULL