Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8172713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:52:28+00:00 2026-06-06T21:52:28+00:00

I have an issue with a search bar I have created, I have no

  • 0

I have an issue with a search bar I have created, I have no errors and I know my query is correct as that my search bar used to work correctly in old mysql code. But since I have try changing my code to mysqli, it hasn’t quite work. No matter what the user enters in the search bar, it always states “Please enter in a phrase within the search bar”, no matter if the search bar is empty or not. It doesn’t display the results of the search as well. So my question is that why doesn’t it show the results of the search if keyword is found or not?

Below is the current code:

        <?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;
        }

    ?>

Here is the link to the application it self for you to test. If you type in “AAA” in search bar, it should display results but it keeps stating that a phrase needs to be entered. IF you enter in something random so that the search should not find a result, it still states please enter in a phrase. Application

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:52:30+00:00Added an answer on June 6, 2026 at 9:52 pm

    I rewrote your code a bit, and switched to PDO for easier bindings.
    Try this

    $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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an iPhone project that uses uses the ZXing bar code scanning
I have a action bar search widget that offers search suggestion too. When the
I have a little issue here. Why does my search bar search when I
I have a global-scale map with a search bar that allows the user to
I have this issue, I'm have a user selected in LDAP through ldap search,
HI all, I have an issue where i have to search rows in a
I have issue with: <form:checkboxes path=roles cssClass=checkbox items=${roleSelections} /> If previous line is used
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
I have a table view with a search bar at the top of the
Recently I have encountered an issue with Scribd where searching via Scribd API (docs.search)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.