【问题标题】:select from multiple tables with jquery autosuggest使用 jquery autosuggest 从多个表中选择
【发布时间】:2012-07-30 11:50:57
【问题描述】:

我正在编写一个网站以了解有关 PHP 的更多信息,并且正在制作 JQuery Autosuggest。脚本 here.

这是我的 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!';
    }
}
?>

现在,我想在另一个表(称为 cat)和 subcat 中搜索 cat 字段,而不是只在公司表中搜索名称strong> 不同表中的字段(在名为 subcat 的表中)

我的表结构:

here(我还不能发图片)

所以我想从三个表中搜索。这可能吗?如何使用我的代码来实现?

感谢大家的帮助!

【问题讨论】:

  • 您是否尝试过搜索所有三个表格,然后将结果合并并打印出来?您是在问是否可以完成,但不要提及您是否一开始就尝试过。

标签: php jquery mysql mysqli


【解决方案1】:

您应该在 3 个表中的每一个之间执行 UNION,并将数据规范化为 NameIDType 列。

【讨论】:

  • 谢谢我解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
  • 2011-07-03
相关资源
最近更新 更多