MySQLi New query in a loop
I would like to get my category list with number of row in product table :
Product : id, title, id_cat
Product_cat : id, name
So I do this
if ($stmt = $mysqli->prepare("SELECT id, name FROM product_cat")) {
$stmt->execute();
$stmt->bind_result($cat_id, $cat_name);
while ($stmt->fetch()) {
if ($stmt2 = $mysqli->prepare("SELECT id FROM product WHERE id_cat = ?
")) {
$stmt2->bind_param('i', $cat_id);
$stmt2->execute();
$stmt2->store_result();
$count_product = $stmt->num_rows;
}
echo '<li><a href="list.php?cat='.$cat_id.'"><span class="badge
pull-right">'.$count_product.'</span>'.utf8_encode($cat_name).'</a></li>';
}
}
$stmt->close();
Why this is not correct? Thanks
No comments:
Post a Comment