2 回答

TA贡献1799条经验 获得超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"]));
}
这会:
执行一次查询以获取所有类别
执行一个查询以获取所有相关产品,但仅针对检索到的类别(在您的情况下,一个简单的方法
select *
可以工作,但如果您稍后需要向第一个选择添加过滤器,则需要缩小范围使用类别构建数组并将 id 设置为键(供以后查找)
根据产品更新数组
它总是只执行 2 次查询而不是 1 + 产品数量,如果您使用准备好的语句,您还可以避免第二次订单注入(如果这可能是一个问题,通常如果id
不是用户定义的,那么它可能不会成为问题)
- 2 回答
- 0 关注
- 124 浏览
添加回答
举报