我的代码应根据要求显示特定类别和品牌的产品。例如,我选择了Apple品牌,它仅显示Apple产品。 但是它不起作用,回顾了Stackoverflow的一半内容以及PHP和MySQL文档。 这是一个错误:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\shop\view_cat.php on line 126
第126行是:
if (mysqli_num_rows($result) > 0 ){
也许我需要用foreach代替do-while循环? 版本PHP 7.3.5,MariaDB 10.1.37
<?php
if (!empty($cat) && !empty($type)) {
$querycat = "AND brand='$cat' AND type_towara='$type'";
} else {
if (!empty($type)) {
$querycat = "AND type_towara='$type'";
} else {
$querycat = "";
}
}
$result = mysqli_query($link,"SELECT * FROM table_products $querycat ORDER BY $sorting");
if (mysqli_num_rows($result) > 0 ){
$row = mysqli_fetch_array($result);
do {
if ($row["image"] != "" && file_exists("./uploads_images/".$row["image"])){
$img_path = './uploads_images/'.$row["image"];
$max_width = 150;
$max_height = 150;
list($width, $height) = getimagesize($img_path);
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min ($ratioh, $ratiow);
$width = intval($ratio*$width);
$height = intval($ratio*$height);
}else{
$img_path = "/shop/images/noimages80x70.png";
$width = 80;
$height = 70;
}
echo ('
<li>
<div class="block-images-list">
<img src="'.$img_path.'" width="'.$width.'" height="'.$height.'"/>
</div>
<ul class="reviews-and-counts-list">
<li><img src="/shop/images/eye-icon.png"/><p>0</p></li>
<li><img src="/shop/images/comment-icon.png"/><p>0</p></li>
</ul>
<p class="style-title-list"><a href="">'.$row["title"].'</a></p>
<a class="add-cart-style-list"></a>
<p class="style-price-list"><strong>'.$row["price"].'</strong> dollars.</p>
<div class="style-text-list">
'.$row["mini_description"].'
</div>
</li>
');
}
while ($row = mysqli_fetch_array($result));
}
?>