2 回答

TA貢獻1833條經(jīng)驗 獲得超4個贊
使用 array_unique 函數(shù)避免列表中的重復(fù)
使用 array_unique 函數(shù)避免列表中的重復(fù)

TA貢獻1799條經(jīng)驗 獲得超6個贊
注意:您的查詢存在N+1 問題。
如果 id 是用戶生成的字符串,它也可能遭受二次 SQL 注入。
這是一個潛在的解決方案:
$sqla = "SELECT * from categoriesb";
$categories = $mysql->Execute($sqla);
$ids = array_column($categories, 'id');
// Query with parameters
$sqlb = "SELECT * from productsb where parent IN (".implode(',', array_fill(0, count($ids), '?')).")";
// Not exactly sure what API is used here but this needs to execute the query that uses the $ids as the parameters
$products = $mysql->Execute($sqlb, $ids);
foreach ($categories as $category) {
$tab[$category['id']] = [ "name" => $category["name_c"] ];
}
foreach ($products as $product) {
$tab[$product['parent']][] = array("product" => $product["name"]));
}
這會:
執(zhí)行一次查詢以獲取所有類別
執(zhí)行一個查詢以獲取所有相關(guān)產(chǎn)品,但僅針對檢索到的類別(在您的情況下,一個簡單的方法
select *
可以工作,但如果您稍后需要向第一個選擇添加過濾器,則需要縮小范圍使用類別構(gòu)建數(shù)組并將 id 設(shè)置為鍵(供以后查找)
根據(jù)產(chǎn)品更新數(shù)組
它總是只執(zhí)行 2 次查詢而不是 1 + 產(chǎn)品數(shù)量,如果您使用準(zhǔn)備好的語句,您還可以避免第二次訂單注入(如果這可能是一個問題,通常如果id
不是用戶定義的,那么它可能不會成為問題)
- 2 回答
- 0 關(guān)注
- 141 瀏覽
添加回答
舉報