我有一个名为 products 的 MySQL 表,在这个表中我有数据:
id | category | product1 | price1 | discount1 | product2 | price2 | discount2 | product3 | price3 |discount3 |
----------------------------------------------------------------------------------------------------------
| 1 | Meat | Pork 1kg | 3.99 | -30% | Lamb 1kg | 5.94 | -25% | Ham 200g | 1.99 | -10% |
| 2 | Fruit | Apple 1kg| 1.25 | -32% | Banana 1 kg | 0.99 | -15% | Melon 1 kg | 0.79 | -12% |
我的 PHP 搜索脚本如下所示:
//-query the database table
$sql="SELECT id, category, product1, price1, discount1,
product2, price2, discount2, product3, price3, discount3
FROM products
WHERE
product1 LIKE '%" . $name . "%' OR
price1 LIKE '%" . $name ."%' OR
discount1 LIKE '%" . $name ."%' OR
product2 LIKE '%" . $name ."%' OR
price2 LIKE '%" . $name ."%' OR
discount2 LIKE '%" . $name ."%' OR
product3 LIKE '%" . $name ."%' OR
price3 LIKE '%" . $name ."%' OR
discount3 LIKE '%" . $name ."%'";
//-run the query against the mysql query function
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$id=$row['id'];
$category =$row['category'];
$product1=$row['product1'];
$price1=$row['price1'];
$discount1=$row['discount1'];
$product2=$row['product2'];
$price2=$row['price2'];
$discount2=$row['discount2'];
$product3=$row['product3'];
$price3=$row['price3'];
$discount3=$row['discount3'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . $id . "</li>\n";
echo "<li>" . $category . "</li>\n";
echo "<li>" . $product1 . "</li>\n";
echo "<li>" . $price1 . "</li>\n";
echo "<li>" . $discount1 . "</li>\n";
echo "<li>" . $product2 . "</li>\n";
echo "<li>" . $price2 . "</li>\n";
echo "<li>" . $discount2 . "</li>\n";
echo "<li>" . $product3 . "</li>\n";
echo "<li>" . $price3 . "</li>\n";
echo "<li>" . $discount3 . "</li>\n";
echo "</ul>";
}
问题是当我搜索让我们说苹果时,香蕉和甜瓜也会显示出来。
使用关键字 apple 搜索时,如何使其仅显示 Apple 及其价格和折扣。