0

作成した検索バーに問題があり、エラーはなく、検索バーが古いmysqlコードで正しく機能していたため、クエリが正しいことがわかりました。しかし、コードをmysqliに変更しようとしたので、うまくいきませんでした。ユーザーが検索バーに何を入力しても、検索バーが空であるかどうかに関係なく、常に「検索バー内にフレーズを入力してください」と表示されます。検索結果も表示されません。だから私の質問は、キーワードが見つかったかどうかにかかわらず、なぜ検索結果が表示されないのですか?

以下は現在のコードです:

        <?php
  $questioncontent = (isset($_POST['questioncontent'])) ? $_POST['questioncontent'] : '';

        $searchquestion = $questioncontent;
        $terms = explode(" ", $searchquestion);


        //Query for search bar      
        $questionquery = "
            SELECT q.QuestionId, q.QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT(an.Answer ORDER BY an.Answer SEPARATOR ' ') AS Answer, r.ReplyType, 
                   q.QuestionMarks 
              FROM Answer an 
              INNER JOIN Question q ON q.AnswerId = an.AnswerId
              JOIN Reply r ON q.ReplyId = r.ReplyId 
              JOIN Option_Table o ON q.OptionId = o.OptionId 

                             WHERE ";

        $i = 0;
        foreach ($terms as $each) {
            $i++;


        //If one term entered in search bar then perform a LIKE statement to look up the term entered       
            if ($i == 1) {
                $questionquery .= "q.QuestionContent LIKE ? ";
            } else {
        //If multiple terms then add an OR statement to check for multiple keywords        
                $questionquery .= "OR q.QuestionContent LIKE ? ";
            }
        }
        //Order by terms entered in ascending order they have been entered               
        $questionquery .= "GROUP BY q.QuestionId, q.SessionId ORDER BY ";
        $i = 0;
        foreach ($terms as $each) {
            $i++;

        //if there are multiple terms, then for example there are 2 terms then display content which contains both terms first, then display content which contains only one of those terms                  
            if ($i != 1)
                $questionquery .= "+";
            $questionquery .= "IF(q.QuestionContent LIKE ? ,1,0)";
        }

        $questionquery .= " DESC ";

        //prepare query, bind the terms and execute query    
        $stmt = $mysqli->prepare($questionquery);
        $stmt->bind_param('ss', $each = '%' . $each . '%', $each = '%' . $each . '%');
        $stmt->execute();
        $stmt->bind_result($dbQuestionId, $dbQuestionContent, $dbOptionType, $dbNoofAnswers, $dbAnswer, $dbReplyType, $dbQuestionMarks);
        $questionnum = $stmt->num_rows();

        //If search bar is empty and user submits the search bar, then below is the phrase it should display:



        if (empty($questioncontent)) {
            echo "Please enter in a phrase in the text box in able to search for a question";
        }

        //Below is the code if no results are found from the search:
        else if ($questionnum == 0) {
            echo "<p>Your Search: '$searchquestion'</p>";
            echo "<p>Number of Questions Shown from the Search: <strong>$questionnum</strong></p>";
            echo "<p>Sorry, No Questions were found from this Search</p>";
        }

        //Finally below is the code that displays the results of the search if search is successful:
        else {
            echo "<p>Your Search: '$searchquestion'</p>";
            echo"<p>Number of Questions Shown from the Search: <strong>$questionnum</strong></p>";

            $output = "";
            $output .= "
                <table border='1' id='resulttbl'>
                  <tr>
                  <th class='questionth'>Question</th>
                  </tr>
            ";
            while ($stmt->fetch()) {
                $output .= "
                  <tr>
                  <td class='questiontd'>{$dbQuestionContent['QuestionContent']}</td>
                  <td class='addtd'><button type='button' class='add' onclick=\"parent.addwindow('{$dbQuestionContent['QuestionContent']}');\">Add</button></td>
                  </tr>";
            }
            $output .= "        </table>";

            echo $output;
        }

    ?>

これは、自分でテストできるアプリケーションへのリンクです。検索バーに「AAA」と入力すると、結果が表示されますが、フレーズを入力する必要があると表示され続けます。検索結果が見つからないようにランダムに入力した場合でも、フレーズを入力してくださいと表示されます。応用

4

1 に答える 1

0

コードを少し書き直し、バインディングを簡単にするためにPDOに切り替えました。これを試して

$searchquestion = $_GET['questioncontent'];
        $terms = explode(" ", $searchquestion);


        //Query for search bar      
        $questionquery = "
            SELECT q.QuestionId, q.QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT(an.Answer ORDER BY an.Answer SEPARATOR ' ') AS Answer, r.ReplyType, 
                   q.QuestionMarks 
              FROM Answer an 
              INNER JOIN Question q ON q.AnswerId = an.AnswerId
              JOIN Reply r ON q.ReplyId = r.ReplyId 
              JOIN Option_Table o ON q.OptionId = o.OptionId 

                             WHERE ";

  $where = array();
  $bindings = array();
  $orderby=array();

    foreach($terms as $key=>$each)
    {
      $where[] = 'q.QuestionContent LIKE ? ';
      $bindings[] = $each;
      $orderby[] = ' IF(q.QuestionContent LIKE ? , 1, 0)';

    }

    $questionquery .= join(' OR ', $where);
    $questionquery .= "GROUP BY q.QuestionId, q.SessionId ORDER BY " . join('+', $orderby) . ' DESC';




     $stmt = $pdo->prepare($questionquery);
    $sth->execute(array_merge($bindings, $bindings));
    $results = $sth->fetchAll()
于 2012-06-22T00:44:49.160 に答える