使用 jquery autosuggest 從多個表中選擇 (select from multiple tables with jquery autosuggest)


問題描述

使用 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 NameID and Type columns.

(by MuhambiDaniel A. White)

參考文件

  1. select from multiple tables with jquery autosuggest (CC BY‑SA 3.0/4.0)

#mysqli #jquery #MySQL #PHP






相關問題

如何第一次創建 mysqli 數據庫? (how can I creates a mysqli database for first the first time?)

使用 jquery autosuggest 從多個表中選擇 (select from multiple tables with jquery autosuggest)

無法連接 - 數據庫或編碼錯誤 (Could Not Connect - Database or Coding Error)

mysqli忽略表中的第一行 (mysqli ignoring the first row in a table)

從數據庫導入的鏈接以 UTF8 截斷 (imported link from database cut off at UTF8)

試圖獲取非對象的屬性 (Trying to get property of non-object)

獲取 php 和 mysqli 中插入數據的 ID (Get Id of inserted data in php and mysqli)

WAMP 和 mysqli::real_connect(): (HY000/2002)? (WAMP and mysqli::real_connect(): (HY000/2002)?)

如何將 MySQLi 準備好的語句的結果放入關聯數組中? (How can I put the results of a MySQLi prepared statement into an associative array?)

測試選擇查詢是否找到結果 (Testing if a Select Query found results)

為什麼我的正確連接嘗試在 apache2 下失敗但在 cli 下失敗? (Why is my correct connection attempt failing under apache2 but not under cli?)

在帶有數組的 IN 語句之後,在 WHERE 語句中使用更多佔位符 (Use more placeholders in WHERE statement after IN statement with arrays)







留言討論