【发布时间】:2015-12-23 11:40:14
【问题描述】:
我在父类别页面上获取产品时遇到问题。 我的数据库表结构:
父类:
id -catname
1 - Mobiles & Tablets
2 - Compuetrs
子类:
id cid sub_name
1 1 Mobiles
2 1 Tablets
3 2 Desktops
4 2 Laptops
产品表结构:我的产品表是多个并基于子类别。示例:平板电脑产品位于 tablets 表下,手机产品位于 mobiles 表下。产品根据其子类别存储在不同的表下。
id cid sub_id product_name
1 1 1 Lenovo latest mobile
2 2 3 Dell Monitor
现在我想从父类别页面上的表格(手机、平板电脑、台式机、笔记本电脑)中获取产品。我用 union all 试过这个,但一次只有一个表在查询中获取。任何人都可以提出一些建议。提前致谢。
<?php
if(isset($_GET)) {
$cid = $_GET['id'];
if($cid == 1){
$tablename = "mobiles";
}
if($cid == 2){
$tablename = "computers";
}
$results=mysql_query("SELECT * FROM $tablename WHERE cid = '$cid'");
while($rows=mysql_fetch_array($results)){
// code
}
}
【问题讨论】: