在我的网站上,有一个搜索框,用户可以输入内容并搜索该词,无论该词是否在mySQL数据库中。
我的代码:
$search_text = $_POST['srch-text'];
$sql = "SELECT * FROM search WHERE content LIKE '%$search_text%';";
$result = mysqli_query($dbcon, $sql);
$number_of_results = $result->num_rows;
如果任何人输入数据库中存在的两个单词,则工作正常。 假设任何人都提供输入“ ABCD XYZ”。在此,数据库中存在ABCD,但数据库中不存在XYZ。然后它不返回任何结果。我想显示ABCD单词的结果。
答案 0 :(得分:0)
您可以使用正则表达式。如果使用空格分隔单词,则需要用# simple query to get top 10 messages from order timeline
query {
order(id: "gid://shopify/Order/618806837270") {
events(first: 100) {
edges {
node {
__typename message
createdAt
id
}
}
}
}
}
替换它们:
[
{
"order": {
"events": {
"edges": [
{
"node": {
"__typename": "BasicEvent",
"message": "Order was placed on XXXXXXX.",
"createdAt": "2018-11-08T22:16:54Z",
"id": "gid://shopify/BasicEvent/9349083824150"
}
},
{
"node": {
"__typename": "BasicEvent",
"message": "Received new order <a href=\"https://XXXXXXX.myshopify.com/admin/orders/618806837270\">#1183</a>.",
"createdAt": "2018-11-08T22:16:55Z",
"id": "gid://shopify/BasicEvent/9349083856918"
}
},
{
"node": {
"__typename": "BasicEvent",
"message": "XXXXXXX fulfilled 1 item from Shopify.",
"createdAt": "2019-02-04T17:26:00Z",
"id": "gid://shopify/BasicEvent/14072023547926"
}
},
{
"node": {
"__typename": "BasicEvent",
"message": "This order was archived.",
"createdAt": "2019-02-04T17:26:00Z",
"id": "gid://shopify/BasicEvent/14072023580694"
}
},
{
"node": {
"__typename": "CommentEvent",
"message": "this is a comment",
"createdAt": "2019-02-04T17:26:15Z",
"id": "gid://shopify/CommentEvent/14072023875606"
}
}
]
}
}
},
{
"cost": {
"requestedQueryCost": 103,
"actualQueryCost": 8,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 992,
"restoreRate": 50
}
}
}
]
您还应该使用参数传递字符串,而不是用用户输入来修饰查询字符串。因此,本示例使用|
作为参数的占位符。
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以将字符串分成两个字符串,并以空格''作为分隔符 这是一个SQL脚本,您可以轻松将其转换为mysql
declare @s as varchar(100)
declare @s1 as varchar(100)
declare @s2 as varchar(100)
set @s='STRING1 STRING2'
declare @n as int
set @n=charindex(' ',@s)
if @n>0
begin
set @s1=substring(@s,1,@n-1)
set @s2=substring(@s,@n+1,len(@s))
end
select @s1 [mystring1],@s2[mystring2]
然后替换查询而不是
$sql = "SELECT * FROM search WHERE content LIKE '%$search_text%';";
作者
$sql = "SELECT * FROM search WHERE content LIKE '%'+@string1+'%' or content LIKE '%'+@string1+'%'
这是等效的mysql脚本
set @s='STRING1 STRING2';
set @n=LOCATE(' ',@s,1);
set @s1=substring(@s,1,@n-1);
set @s2=substring(@s,@n+1,LENGTH(@s));
select @s1 "mystring1",@s2"mystring2";