【发布时间】:2009-08-18 09:14:07
【问题描述】:
我有以下内容可以抓取父类别下的所有类别:
<?php
$childrows = array();
$result2 = mysql_query("SELECT * FROM categories WHERE category_parent_id='$cid'"); while
($row = mysql_fetch_array($result2)){
$childrows [] = $row['category_id'];
print_r($childrows); }
?>
这会返回:
Array ( [0] => 47 ) Array ( [0] => 47 [1] => 48 ) Array ( [0] => 47 [1] => 48 [2] => 63 ) Array ( [0] => 47 [1] => 48 [2] => 63 [3] => 64 ) Array ( [0] => 47 [1] => 48 [2] => 63 [3] => 64 [4] => 68 ) Array ( [0] => 47 [1] => 48 [2] => 63 [3] => 64 [4] => 68 [5] => 69 )
我遇到的问题是下一个阶段,我需要从 products 表中计算存储在数组中的 category(category_id) 下的 products(product_id) 的数量。
一定有办法!!??
感谢收看。 B.
完整代码:
<?php
if ($num_rows == 0) {
$result2 = mysql_query("SELECT * FROM categories WHERE category_parent_id='$cid'");
while ($row = mysql_fetch_array($result2)){
$childrows [] = $row['category_id'];
}
$category_string = implode(",",$childrows);
$result3 = mysql_query("SELECT category_id, COUNT(product_id)
FROM products
WHERE category_id IN ($in_string)");
$result3 = mysql_fetch_array($result3);
$result3 = $result3[1];
echo $result3;
}
else {
echo $num_rows;
}
?>
但这只是重复:
注意:未定义变量:in_string in on line 31(WHERE category_id IN ($in_string)");)
警告:mysql_fetch_array():提供的参数不是第 32 行上的有效 MySQL 结果资源 ($result3 = mysql_fetch_array($result3);)
【问题讨论】:
-
你需要获取它们.... $result3 = mysql_query(...);回声 $result3;
-
但这就是我所做的……不是吗?