[英]I can't figure out how to function selected categories using $where in SQL
我想顯示從表中選擇的查詢
<?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";
?>
看起來您犯了個小錯誤,因為在“默認”查詢中您已經有WHERE
語句,因此在if中您應該再次使用AND
或OR
而不是WHERE
:
$where = "";
if(isset($_GET['category']))
{
$catid=$_GET['category'];
$where = " AND product.categoryid = $catid";
}
另外,通過使用sprintf()函數,可以使注入其他WHERE
條件更加優雅。 看一個例子:
$additional_condition = 'AND active=1'
$statement = 'SELECT * FROM users WHERE condition=1 %s';
echo sprintf($statement, $additional_condition);
// Output: SELECT * FROM users WHERE condition=1 AND active=1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.