我无法弄清楚如何在SQL中使用$ where来选择类别

时间:2018-12-06 09:17:21

标签: php mysql sqlyog

我想显示从表中选择的查询

<?php 
    $where = "";

    if(isset($_GET['category']))
    {
        $catid = $_GET['category'];
        $where = " WHERE product.categoryid = $catid";
    }

因此是$where函数,用于获取类别

 $sql = "SELECT category.*,  
             category.category_id , items.id AS itemID,items.asset_tag,items.name,
                  items.brand,items.status,
                  items.quantity,items.category_id,
                items.color,items.texture,items.photo,items.date,
             items.fetch_item FROM items  LEFT JOIN category ON 
                     category.category_id=items.category_id 
                  WHERE  items.status = 'Available' AND items.fetch_item = '0' $where";


         ?>

1 个答案:

答案 0 :(得分:2)

看起来您犯了个小错误,因为在“默认”查询中您已经有WHERE语句,因此在if中您应该使用ANDOR而不是{{1 }}:

WHERE

此外,您可以使用sprintf()函数使注入其他$where = ""; if(isset($_GET['category'])) { $catid=$_GET['category']; $where = " AND product.categoryid = $catid"; } 条件更加优雅。看一个例子:

WHERE