Php Foreach循环次数过多
我用它来显示Wordpress中查询数据库中的信息。它显示正确的信息,但循环次数太多。它被设置为从SELECT查询显示,并且取决于数据库的最后一个条目,它似乎是打印每个条目的两倍还是三倍Php Foreach循环次数过多,php,wordpress,loops,foreach,Php,Wordpress,Loops,Foreach,我用它来显示Wordpress中查询数据库中的信息。它显示正确的信息,但循环次数太多。它被设置为从SELECT查询显示,并且取决于数据库的最后一个条目,它似乎是打印每个条目的两倍还是三倍 foreach ($result as $row) { echo '<h5><i>'.$row->company.'</i> can perform your window installation for <i>$'.$row->cost.'<
foreach ($result as $row) {
echo '<h5><i>'.$row->company.'</i> can perform your window installation for <i>$'.$row->cost.'</i><br>';
echo 'This price includes using<i> '.$row->material.'</i> as your material(s)<br>';
echo '<hr></h5>';
}
$result = $wpdb->get_results( "SELECT bp.*, b.company
FROM `windows_brands_products` bp
LEFT JOIN `windows_brands` b
ON bp.brand_id = b.id
JOIN Windows_last_submissions ls
JOIN windows_materials wm
JOIN Windows_submissions ws
WHERE ws.username = '$current_user->user_login'
AND bp.width = ROUND(ls.width)
AND bp.height = ROUND(ls.height)
AND bp.material IN (wm.name)
AND bp.type = ls.type
AND IF (ls.minimumbid != '0.00',bp.cost BETWEEN ls.minimumbid AND ls.maximumbid,bp.cost <= ls.maximumbid)
ORDER BY b.company ASC");
foreach($result作为$row){
echo'.$row->company.'可以用$row->cost执行窗口安装。
;
echo“此价格包括使用“$row->material.”作为您的材料
;
回声“
”;
}
有人知道是什么导致了这个错误吗?
谢谢
$result = $wpdb->get_results( "SELECT bp.*, b.company
FROM `windows_brands_products` bp
LEFT JOIN `windows_brands` b
ON bp.brand_id = b.id
JOIN Windows_last_submissions ls
JOIN windows_materials wm
JOIN Windows_submissions ws
WHERE ws.username = '$current_user->user_login'
AND bp.width = ROUND(ls.width)
AND bp.height = ROUND(ls.height)
AND bp.material IN (wm.name)
AND bp.type = ls.type
AND IF (ls.minimumbid != '0.00',bp.cost BETWEEN ls.minimumbid AND ls.maximumbid,bp.cost <= ls.maximumbid)
ORDER BY b.company ASC");
为该脚本提供动力的查询是:
$result = $wpdb->get_results( "SELECT bp.*, b.company
FROM `windows_brands_products` bp
LEFT JOIN `windows_brands` b
ON bp.brand_id = b.id
JOIN Windows_last_submissions ls
JOIN windows_materials wm
JOIN Windows_submissions ws
WHERE ws.username = '$current_user->user_login'
AND bp.width = ROUND(ls.width)
AND bp.height = ROUND(ls.height)
AND bp.material IN (wm.name)
AND bp.type = ls.type
AND IF (ls.minimumbid != '0.00',bp.cost BETWEEN ls.minimumbid AND ls.maximumbid,bp.cost <= ls.maximumbid)
ORDER BY b.company ASC");
$result=$wpdb->get_results(“选择bp.*,b.company
来自“windows\u品牌\u产品”bp
左键连接'windows_brands'b
关于bp.brand_id=b.id
加入Windows\u last\u提交
加入windows\u材质wm
加入Windows\u提交
其中ws.username='$current\u user->user\u login'
和bp.width=圆形(ls.width)
和bp.height=圆形(ls.height)
和bp.material IN(wm.name)
和bp.type=ls.type
如果(ls.minimumbid!=“0.00”,则ls.minimumbid和ls.maximumbid之间的bp.cost,bp.cost如果您使用MySQLi,我认为您需要以下内容:
$result = $wpdb->get_results( "SELECT bp.*, b.company
FROM `windows_brands_products` bp
LEFT JOIN `windows_brands` b
ON bp.brand_id = b.id
JOIN Windows_last_submissions ls
JOIN windows_materials wm
JOIN Windows_submissions ws
WHERE ws.username = '$current_user->user_login'
AND bp.width = ROUND(ls.width)
AND bp.height = ROUND(ls.height)
AND bp.material IN (wm.name)
AND bp.type = ls.type
AND IF (ls.minimumbid != '0.00',bp.cost BETWEEN ls.minimumbid AND ls.maximumbid,bp.cost <= ls.maximumbid)
ORDER BY b.company ASC");
while ($row = $result->fetch_object()) {
echo '<h5><i>'.$row->company.'</i> can perform your window installation for <i>$'.$row->cost.'</i><br>';
echo 'This price includes using<i> '.$row->material.'</i> as your material(s)<br>';
echo '<hr></h5>';
}
while($row=$result->fetch_object()){
echo'.$row->company.'可以用$row->cost执行窗口安装。
;
echo“此价格包括使用“$row->material.”作为您的材料
;
回声“
”;
}
我的查询中有冗余的JOIN子句,它从两个表(其中一个只是另一个表的视图)中提取了几乎相同的结果.您能向我们展示一下您的查询和数据库架构的相关部分吗?我猜您的查询中可能有一些重复的行,可能来自联接。foreach
不是这里的问题,我可以保证。太模糊了,需要更多的信息。也不认为您可以使用foreach()
在result对象上。将查看fetch_assoc()
类似函数的$result
,例如:while($row=$result->fetch_assoc()){}
@Twisty-$result可能只是一个对象数组,它完全有效,可以像任何其他数组一样进行迭代-此外,实现Iterable
的对象可以使用foreach()进行迭代
,以及生成器
请您的问题包括触发双项或三项(或单项,如果有)的DB项示例。感谢您提高了问题的参考值并使其更易于回答!@MarkBaker如果OP没有提到执行选择
,我会同意的。这建议使用db结果对象,而不是对象数组。此外,OP没有定义他正在使用的SQL函数。我不是在争论您的观点,我同意,但我提出了su根据帖子和标签的上下文进行分类。
$result = $wpdb->get_results( "SELECT bp.*, b.company
FROM `windows_brands_products` bp
LEFT JOIN `windows_brands` b
ON bp.brand_id = b.id
JOIN Windows_last_submissions ls
JOIN windows_materials wm
JOIN Windows_submissions ws
WHERE ws.username = '$current_user->user_login'
AND bp.width = ROUND(ls.width)
AND bp.height = ROUND(ls.height)
AND bp.material IN (wm.name)
AND bp.type = ls.type
AND IF (ls.minimumbid != '0.00',bp.cost BETWEEN ls.minimumbid AND ls.maximumbid,bp.cost <= ls.maximumbid)
ORDER BY b.company ASC");