あなたがラップしていないので、あなたの内側のクエリが失敗している理由はまず、与えられた、パラメータ1がmysqli_resultされることを想定してい{$outerRow["manufacturer_name"]}
を文字列値として単一引用符で囲む必要があり、サブクエリのかっこは閉じないでください:'{$outerRow["manufacturer_name"]}');
しかし、私はあなたのクエリを修正したくありません。読んでください。
私はあなたのプロセスについて修正したいのですがいくつかあります。
- あなたの最初のクエリが
MySQL
の真の力を利用していないが。あなたは本当にあなたのデータベース内の各メーカーのために新しいクエリを作成するべきではありません - それは非効率です。
- 警告メッセージを避け、誤ったクエリを適切に処理するには、クエリの結果セットを宣言し、条件チェックすることをお勧めします。
- domビルド中に
<optgroup>
タグを閉じているわけではありません。あなたの出力は望みどおりにレンダリングされますが、タグは閉じられます。
- 結果セットの数値キーを使用していないため、
mysqli_fetch_array()
の代わりにmysqli_fetch_assoc()
を使用する必要があります。
これは、上記の修正を行い、単一の呼び出しにあなたのクエリプロセスをダウン洗練されますあなたのコードを完全に書き直し、次のとおりです。
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;
私のコードブロックが出力されます。この:
P.S.:などをレンダリング
<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>
結果セットに製品レスの製造元(またはオプションリストを選択)が必要ない場合は、GROUP BY
とORDER BY
の行/句の間にHAVING P.product_id IS NOT NULL
と書いてください。
'$ outerResults ='と '$ innerResults ='文の前に '$ query;'を出力できますか? – Manngo