我尝试使用 db 的结果进行自动完成搜索。我使用此代码来获取名称,在 db 中,我有一行可以包含搜索的关键字的四列。
function search_results($conn,$str){
$sql = "SELECT g_custom_1 as str FROM gallery WHERE g_custom_1 LIKE '%{$str}%'";
$sql .= "SELECT g_custom_2 as str FROM gallery WHERE g_custom_2 LIKE '%{$str}%'";
$sql .= "SELECT g_custom_3 as str FROM gallery WHERE g_custom_3 LIKE '%{$str}%'";
$sql .= "SELECT g_custom_4 as str FROM gallery WHERE g_custom_4 LIKE '%{$str}%'";
//$sub_data=array();
if (mysqli_multi_query($conn,$sql))
{
$data=array();
do
{
// Store first result set
if ($result=mysqli_store_result($conn)) {
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$data[]=$row['str'];
}
// Free result set
mysqli_free_result($result);
}
}
while (mysqli_next_result($conn));
}
else{
echo "error";
}
$data_unique=array_unique($data);
return $data_unique;
}