无法管理从PHP中的类别显示产品

时间:2013-02-08 13:23:55

标签: php mysql

<?php 
// This block grabs the whole list for viewing
include "storescripts/connect_to_mysql.php";
$cat=$_POST['cat'];
$sql = mysql_query("select * from categories,products_cat,products where categories.id=products_cat.cat_id and products.id=products_cat.prod_id and categories=$cat");
$catCount = mysql_num_rows($sql); // count the output amount
    while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $prod_list .= "category ID: $id - <strong>$product_name</strong> <br />";
    }

?>

我有这三个表: 1)产品表: id,product_name,price,category,subcategory

2)类别表: ID,猫

3)products_cat表: cat_id,prod_id

我无法显示某些类别的产品,用户将从命令行输入cat = x,它只会给我这个错误:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/perzul12/public_html/beta/show.php on line 6

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/perzul12/public_html/beta/show.php on line 7

请帮帮我!这是我本月唯一的资金来源,我无法做到这一点!

1 个答案:

答案 0 :(得分:0)

此处的工作不在弃用的mysql_函数中。相反,您尚未返回有效的结果集。试试这个。

"select products_cat.products, categories.* 
from categories,
left join products_cat on products_cat.cat_id=products.id
where categories={$cat}"