我想创建一个搜索框,例如Google搜索框。
我的数据库中有一个表格,其中存储了搜索文本。
---------------------------
| searchdata | output |
---------------------------
| id | id=-1 |
| Data for id 1 | id=1 |
| data for 2 | id=2 |
| datatx id 2 | id=4 |
| datacv for id | id=5 |
---------------------------
现在在我的php文件中
<?php
if ($con = mysqli_connect("localhost","root","root","search")) {
$chatData1 = $_POST['request'];
$textm= str_replace(" ", "%' or searchdata like '%", $chatData1);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM search where searchdata like '%{$textm}%' ");
if($row = mysqli_fetch_array($result))
{
echo $row['output'];
}
else{
echo "Please use English(US) Language.";
}
}
mysqli_close($con);
?>
例如,如果用户键入data for id
sql查询必须像这样
SELECT *
FROM search
WHERE searchdata LIKE '%data%' OR
searchdata LIKE '%for%' OR
searchdata LIKE '%id%'`
所以它必须给出输出acc。表id=1
,但它给我输出id=-1
答案 0 :(得分:0)
您可以使用jQuery Ui Autocomplete解决此问题,只需指定字段并使用JSON显示数据
$data = array()
if($row = mysqli_fetch_array($result))
{
data[] = $row['output'];
}
// Outputs JSON
print json_encode($data);