Mysqli multi_query搜索2个表未显示结果

时间:2013-09-18 20:40:19

标签: php database mysqli

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

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

// 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存储关键字/标签名称&#39; Keyword_Name&#39;。

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

1 个答案:

答案 0 :(得分:1)

我假设:

$ con由

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

mysqli_multi_query:除了最后一个命令外,所有sql命令都必须以;终止。 并$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