【问题标题】:Mysqli multi_query Search of 2 Tables Not showing ResultsMysqli multi_query 搜索 2 个表不显示结果
【发布时间】:2013-09-18 20:40:19
【问题描述】:

我竭力想弄清楚为什么我的查询不起作用。这就是我的 search.php 页面的结果。我能够完美地 _GET 搜索词,但无法显示结果。

不确定问题是 fetch_array_assoc 还是什么!这是我的代码。对此的任何帮助将不胜感激。不是 100% 确定我的语法是否正确。

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$button = $_GET ['submit'];
$search = $_GET ['query'];

if (strlen($search) <= 1) {
    echo "Search term too short";
}
else {
    echo "You searched for <b>$search</b> <hr size='1'></br>";
    $con = new mysqli("localhost", "user", "pass", "db");

    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $search_exploded = explode(" ", $search);
    foreach ($search_exploded as $search_each) {
        $x++;
        if ($x == 1) {
            $query = "Keyword_ID LIKE '%$search_each%' or Keyword_Name LIKE '%$search_each%' ";
        }
        else {
            $query .= "OR Keyword_ID LIKE '%$search_each%' or Keyword_Name LIKE '%$search_each%' ";
        }
    }
    $construct = mysqli_query($con, "SELECT * FROM profileTable WHERE $query");
    $construct = mysqli_query($con, "SELECT * FROM addKeywordTable (Keyword_Name) WHERE $query");
    $constructs = mysqli_multi_query($construct);

    if (mysqli_multi_query($construct)) {
        $numrows = mysqli_num_rows($query);
        if ($numrows > 0) {
            while ($row = mysqli_fetch_assoc($constructs)) {
                $key = $row['Keyword_Name'];
                $keyID = $row['keyID'];
                $fname = $row['FirName'];
                $lname = $row['LaName'];
                $mname = $row['MName'];
                $suffix = $row['Suffix'];
                $title = $row['Title'];
                $dept = $row['Dept'];
                $phone1 = $row['PH1'];
                $phone2 = $row['PH2'];
                $email = $row['Email'];
                $photo = $row['Photo'];
                $bio = $row['BioLK'];
                $tags = $row['Tags'];

                echo '<h2>$fname $lname</h2>';
                echo $key;
                echo $title;
                echo $dept;
            }
        }
        else {
            echo "Results found: \"<b>$x</b>\"";
        }
    }
}
mysqli_close();
?> 

我正在尝试搜索两个不同的表。 addKeywordTable 和 profileTable。配置文件表包含用户的所有配置文件信息。 addKeywordTable 存储关键字/标签名称“Keyword_Name”。

我试图创建一个 mysqli_multi_query 但它根本不起作用。

【问题讨论】:

  • 我不认为你正在生成你认为你正在生成的 SQL。尝试用伪代码写出逻辑,并编写您认为要提交的实际 SQL。
  • 另外,您的数据库架构以及您获得的实际输出。
  • 感谢@FrankieTheKneeMan,但这很明显。对此的任何帮助都会很棒。谢谢...
  • 帮助我们帮助您 - 我向您询问了四条信息,以便更好地诊断手头的问题,而不仅仅是为了好玩。
  • 我看不出你在哪里向我询问了四条信息。为什么这么沮丧?

标签: php database mysqli


【解决方案1】:

我假设:

$con 由

设置
$con = mysqli_connect("host", "user", "password", "db");

mysqli_multi_query : 你必须所有的 sql 命令,除了最后一个,以;结束
并 concenat $construct.= 。否则你会覆盖你的$construct

 $construct  = "SELECT * FROM profileTable WHERE $query ;");
 $construct .= "SELECT * FROM addKeywordTable (Keyword_Name) WHERE $query");

不要用

设置 $construct
$construct = mysqli_query($con, "SELECT * FROM profileTable WHERE $query");

您的 $construct 只会变为 TRUE 或 FALSE 。
带有内容的变量TRUE or FALSE你不能调用

$constructs = mysqli_multi_query($con,TRUE);

你说错了

$constructs = mysqli_multi_query($construct);

正确

$constructs = mysqli_multi_query($con,$construct);

你给mysqli_multi_query($construct)打了两次电话

$constructs = mysqli_multi_query($construct);
if (mysqli_multi_query($construct)) { ...

不需要第一次调用。

只调用它

if (mysqli_multi_query($con,$construct)) { ...

完全错误是

if (mysqli_multi_query($construct)) {
    $numrows = mysqli_num_rows($query);
      if ($numrows > 0) {
            while ($row = mysqli_fetch_assoc($constructs)) {

$query 目前是一个简单的“字符串”

$query = "Keyword_ID LIKE '%$search_each%' or Keyword_Name LIKE '%$search_each%' ";

也错了

while ($row = mysqli_fetch_assoc($constructs)) {

To retrieve the resultset from the first query you can use mysqli_use_result() or mysqli_store_result(). All subsequent query results can be processed using mysqli_more_results() and mysqli_next_result().

改为这样称呼

if (mysqli_multi_query($con,$construct)) {
   if ($result = mysqli_store_result($con)) {
        while ($row = mysqli_fetch_row($result)) {
            printf("%s\n", $row[0]);
        }
        mysqli_free_result($result);

在设置 $x 之前设置 $x++

$x = 0;

您不能确定 $x 总是自动设置为 0

【讨论】: