我不知道如何使用 SQL 中的 $where 来处理选定的类别
I can't figure out how to function selected categories using $where in SQL
我想显示从 table
中选择的查询
<?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";
?>
看起来你犯了一个小错误,因为在你的 "default" 查询中你已经有 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
我想显示从 table
中选择的查询<?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";
?>
看起来你犯了一个小错误,因为在你的 "default" 查询中你已经有 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