3

I am getting this error in my script which I think is causing the search bar not to work:

Fatal error: Call to a member function bind_param() on a non-object in /web/stud/xxx/Mobile_app/previousquestions.php on line 89.

The line it is pointing to is this line:

$stmt->bind_param("s",$each);    

What needs to be done in order to fix this error? At the moment the error is causing no results to appear after the user has submitted the content within the search bar.

  <?php

        //connect to db

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

        ?>

        <?php 

        if (isset($_GET['searchQuestion'])) {

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

        $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 ($i == 1){         
                $questionquery .= "q.QuestionContent LIKE ?";     
                } else {         
                    $questionquery .= "OR q.QuestionContent LIKE ?";    
                     } 
                     }  

                     $questionquery .= "GROUP BY q.QuestionId, q.SessionId ORDER BY "; $i = 0; foreach ($terms as $each) {     
                         $i++;      

            if ($i != 1)         
            $questionquery .= "+";     
            $questionquery .= "IF(q.QuestionContent LIKE ?,1,0)"; 
            } 

            $questionquery .= " DESC "; 

            $stmt=$mysqli->prepare($questionquery);      
    $parameters[] = ($each)   
    $stmt->execute($parameters);  
            $stmt->bind_result($dbQuestionId,$dbQuestionContent,$dbOptionType,$dbNoofAnswers,$dbAnswer,$dbReplyType,$dbQuestionMarks); 
            $questionnum = $stmt->num_rows();

        }

    ?>
user1394925
  • 754
  • 9
  • 28
  • 51

1 Answers1

2

It means that the $stmt variable isn't being set properly - I think you need spaces after the LIKE ? lines, as you're concatening GROUP BY right after it.

You need to check that the SQL statement you're generating is correct, and that the database connection is also working.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
  • I added a space after LIKE ? statements and now I am getting this warning: `Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement in ... on line 89` Query is correct and connection is fine, but it just doesn't want to work :( – user1394925 Jun 19 '12 at 18:01
  • You've also got bound parameters as part of the WHERE clause, but you're only ever passing in one bound parameter. – andrewsi Jun 19 '12 at 18:03
  • So can I ask what I need to change the bind_param to? – user1394925 Jun 19 '12 at 18:05
  • It's not that you need to change it; you need to pass in a value for each bound parameter in your SQL. You're only passing one in at the moment, no matter how many bound parameters you're using. – andrewsi Jun 19 '12 at 18:09
  • I am not quite understanding what I need to do to solve this? What do I actually need to do? – user1394925 Jun 19 '12 at 18:13
  • When you include `?` as a placeholder in your SQL, you need to use the value of that placeholder using bind_param before your query executes. The query that you're generating has a couple of places where you have placeholders, so for each `?`, you need to specify what the value to use actually is, using bind_param. – andrewsi Jun 19 '12 at 18:18
  • Oh, I think I get it now, as I have 3 ? in my full query, I need to state I am binding three parameters like this:`$stmt->bind_param("sss",$each,$each,$each);` rather than only biding one parameter which im doing at moment – user1394925 Jun 19 '12 at 18:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12754/discussion-between-andrewsi-and-user1421767) – andrewsi Jun 19 '12 at 18:22