[英]fetch data from two tables in php without joining the tables
我仍在學習PHP和MySql,並且在搜索欄上遇到困難。 我的問題是我能夠從數據庫中選擇兩個表,但是我在while循環中遇到了麻煩,在while循環中它會將所有內容扔到搜索欄上,有時什么也沒有。 我為此使用typeahead.js插件。 我希望這些國家首先出現,然后再建議域名,但我不想加入表格。 請幫忙。
這是我的腳本 :
<script>
$(document).ready(function(){
$('input.typeahead').typeahead({
name: 'typeahead',
remote:'search2.php?key=%QUERY',
limit : 30
});
});
</script>
這是我的PHP:
<?php
$key=$_GET['key'];
$array = array();
$con=mysql_connect("localhost","root","");
$db=mysql_select_db("test_db",$con);
$query=mysql_query("select * from tbl_sample where country LIKE '%{$key}%' AND domain LIKE '%{$key}%' ");
while($row=mysql_fetch_assoc($query)){
$array[] = $row['country'];
$array[] = $row['domain'];
}
echo json_encode($array);
?>
鑒於您根本沒有向我們描述第二張表,因此您要問的內容有點含糊,所以我假設您只想從同一張表中進行兩個單獨的選擇。 這樣做,將把國家放在第一位:
$query=mysql_query("select country as 'result' from tbl_sample where country LIKE '%{$key}%' UNION select domain as 'result' from tbl_sample where domain LIKE '%{$key}%' ");
while($row=mysql_fetch_assoc($query)){
$array[] = $row['result'];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.