在多行中搜索 SQL LIKE 中的字符串
Search string in SQL LIKE in multiple rows
我正在尝试进行 "related products" 查询以搜索 5 个相关产品,但出现以下错误:
错误图片:http://puu.sh/eeo5n/a845ef5ba3.png
这是我的代码
// Info current t-shirt. Normally from database
$name = "Luftrauser T-shirt";
$description = "Stylish LUFTRAUSERS T-shirt designed by Amon26.";
$relatedSearch = $name . $description;
// Query which searches for related items
$query = $db->prepare("SELECT * FROM tbl_products WHERE name, description LIKE '%:relatedSearch%'");
$query -> bindParam("relatedSearch", $relatedSearch, PDO::PARAM_STR);
if($query -> execute()) {
while($related = $query->fetch(PDO::FETCH_OBJ)) {
echo $related->title . "</br>";
}
}
为什么 return 包含我使用 LIKE 搜索的值的行?
如果您希望搜索在名称和描述中查找并匹配其中一个
SELECT *
FROM tbl_products
WHERE description LIKE '%:relatedSearch%' OR name LIKE '%:relatedSearch%'
如果您希望搜索查找名称和描述并匹配两者
SELECT *
FROM tbl_products
WHERE description LIKE '%:relatedSearch%' AND name LIKE '%:relatedSearch%'
我正在尝试进行 "related products" 查询以搜索 5 个相关产品,但出现以下错误: 错误图片:http://puu.sh/eeo5n/a845ef5ba3.png
这是我的代码
// Info current t-shirt. Normally from database
$name = "Luftrauser T-shirt";
$description = "Stylish LUFTRAUSERS T-shirt designed by Amon26.";
$relatedSearch = $name . $description;
// Query which searches for related items
$query = $db->prepare("SELECT * FROM tbl_products WHERE name, description LIKE '%:relatedSearch%'");
$query -> bindParam("relatedSearch", $relatedSearch, PDO::PARAM_STR);
if($query -> execute()) {
while($related = $query->fetch(PDO::FETCH_OBJ)) {
echo $related->title . "</br>";
}
}
为什么 return 包含我使用 LIKE 搜索的值的行?
如果您希望搜索在名称和描述中查找并匹配其中一个
SELECT *
FROM tbl_products
WHERE description LIKE '%:relatedSearch%' OR name LIKE '%:relatedSearch%'
如果您希望搜索查找名称和描述并匹配两者
SELECT *
FROM tbl_products
WHERE description LIKE '%:relatedSearch%' AND name LIKE '%:relatedSearch%'