問題描述
使用 jquery autosuggest 從多個表中選擇 (select from multiple tables with jquery autosuggest)
I'm coding a website to learn more about PHP and am making a JQuery Autosuggest. The scripts here.
Here's my code for the autosuggest.php:
<?php
$db = new mysqli('localhost', 'root' ,'***********', '**********');
if(!$db) {
echo 'Could not connect to the database.';
} else {
if(isset($_POST['queryString'])) {
$queryString = $db‑>real_escape_string($_POST['queryString']);
if(strlen($queryString) >0) {
$query = $db‑>query("SELECT * FROM company WHERE name LIKE '$queryString%' LIMIT 10");
if($query) {
echo '<ul>';
while ($result = $query ‑>fetch_object()) {
echo '<li onClick="fill(\''.addslashes($result‑>name).'\');">'.$result‑>name.'</li>';
}
echo '</ul>';
} else {
echo 'OOPS we had a problem :(';
}
} else {
// do nothing
}
} else {
echo 'There should be no direct access to this script!';
}
}
?>
Now, instead of just searching for the name in the company table, I'd like to search for the cat field in a different table (called cat) and the subcat field in a different table than that ( in a table called subcat)
My table structure:
here (I can't post pictures yet)
So I want to search from three tables. Is this possible and how can I do it with my code?
Thanks for all help!
‑‑‑‑‑
參考解法
方法 1:
You should do a UNION
between each of the 3 tables and normalize the data into Name
, ID
and Type
columns.
(by Muhambi、Daniel A. White)