1

我竭力想弄清楚为什么我的查询不起作用。这就是我的 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 但它根本不起作用。

4

1 回答 1

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.

于 2013-09-18T21:55:41.253 回答