Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result

Discussion in 'PHP' started by Matt Ridge, Oct 4, 2011.

  1. #1
    This is the code.

    
      // Grab the response data from the database to generate the form
      $query = "SELECT mr.response_id, mr.topic_id, mr.response, mt.name AS topic_name, mc.name AS category_name " .
        "FROM mismatch_response AS mr " .
        "INNER JOIN mismatch_topic AS mt USING (topic_id) " .
        "INNER JOIN mismatch_category AS mc USING (category_id) " .
        "WHERE mr.user_id = '" . $_SESSION['user_id'] . "'";
    	$data = mysqli_query($dbc, $query);
    	$responses = array();
    	while ($row = mysqli_fetch_array($data)) {
        array_push($responses, $row);
      }
    Code (markup):
    Line 61 is: while ($row = mysqli_fetch_array($data)) {

    This is all of the code.
    
    <?php
      // Start the session
      require_once('startsession.php');
    
    
      // Insert the page header
      $page_title = 'Questionnaire';
      require_once('header.php');
    
    
      require_once('appvars.php');
      require_once('connectvars.php');
    
    
      // Make sure the user is logged in before going any further.
      if (!isset($_SESSION['user_id'])) {
        echo '<p class="login">Please <a href="login.php">log in</a> to access this page.</p>';
        exit();
      }
    
    
      // Show the navigation menu
      require_once('navmenu.php');
    
    
      // Connect to the database
      $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    
    
      // If this user has never answered the questionnaire, insert empty responses into the database
      $query = "SELECT * FROM mismatch_response WHERE user_id = '" . $_SESSION['user_id'] . "'";
      $data = mysqli_query($dbc, $query);
      if (mysqli_num_rows($data) == 0) {
        // First grab the list of topic IDs from the topic table
        $query = "SELECT topic_id FROM mismatch_topic ORDER BY category_id, topic_id";
        $data = mysqli_query($dbc, $query);
        $topicIDs = array();
        while ($row = mysqli_fetch_array($data)) {
          array_push($topicIDs, $row['topic_id']);
        }
    
    
        // Insert empty response rows into the response table, one per topic
        foreach ($topicIDs as $topic_id) {
          $query = "INSERT INTO mismatch_response (user_id, topic_id) VALUES ('" . $_SESSION['user_id']. "', '$topic_id')";
          mysqli_query($dbc, $query);
        }
      }
    
    
      // If the questionnaire form has been submitted, write the form responses to the database
      if (isset($_POST['submit'])) {
        // Write the questionnaire response rows to the response table
        foreach ($_POST as $response_id => $response) {
          $query = "UPDATE mismatch_response SET response = '$response' WHERE response_id = '$response_id'";
          mysqli_query($dbc, $query);
        }
        echo '<p>Your responses have been saved.</p>';
      }
    
    
      // Grab the response data from the database to generate the form
      $query = "SELECT mr.response_id, mr.topic_id, mr.response, mt.name AS topic_name, mc.name AS category_name " .
        "FROM mismatch_response AS mr " .
        "INNER JOIN mismatch_topic AS mt USING (topic_id) " .
        "INNER JOIN mismatch_category AS mc USING (category_id) " .
        "WHERE mr.user_id = '" . $_SESSION['user_id'] . "'";
    	$data = mysqli_query($dbc, $query);
    	$responses = array();
    	while ($row = mysqli_fetch_array($data)) {
        array_push($responses, $row);
      }
    
    
      mysqli_close($dbc);
    
    
      // Generate the questionnaire form by looping through the response array
      echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
      echo '<p>How do you feel about each topic?</p>';
      $category = $responses[0]['category_name'];
      echo '<fieldset><legend>' . $responses[0]['category_name'] . '</legend>';
      foreach ($responses as $response) {
        // Only start a new fieldset if the category has changed
        if ($category != $response['category_name']) {
          $category = $response['category_name'];
          echo '</fieldset><fieldset><legend>' . $response['category_name'] . '</legend>';
        }
    
    
        // Display the topic form field
        echo '<label ' . ($response['response'] == NULL ? 'class="error"' : '') . ' for="' . $response['response_id'] . '">' . $response['topic_name'] . ':</label>';
        echo '<input type="radio" id="' . $response['response_id'] . '" name="' . $response['response_id'] . '" value="1" ' . ($response['response'] == 1 ? 'checked="checked"' : '') . ' />Love ';
        echo '<input type="radio" id="' . $response['response_id'] . '" name="' . $response['response_id'] . '" value="2" ' . ($response['response'] == 2 ? 'checked="checked"' : '') . ' />Hate<br />';
      }
      echo '</fieldset>';
      echo '<input type="submit" value="Save Questionnaire" name="submit" />';
      echo '</form>';
    
    
      // Insert the page footer
      require_once('footer.php');
    ?>
    Code (markup):
    Now I am looking at this code and can't figure out why line 61 is giving me an error.

    Can someone help me, I've been staring at this line for the past hour with no real effect.
     
    Solved! View solution.
    Matt Ridge, Oct 4, 2011 IP
  2. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #2
    Have you tried to get the error message returned from the MySQL server?

    Try to replace this line:

    $data = mysqli_query($dbc, $query);
    PHP:
    with this one:

    $data = mysqli_query($dbc, $query) or die( 'MySQLi Error: ' .mysqli_error($dbc) );
    PHP:
    It should tell you what's the problem ;)
     
    bogi, Oct 4, 2011 IP
  3. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #3
    I tried that already, and the script told me that the line added in was actually where the error was.

    Which seems odd to me.
     
    Matt Ridge, Oct 4, 2011 IP
  4. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #4
    Strange, it should describe the error. Could you copy-paste the error message once you replace that line in your code to the one I suggested above?
     
    bogi, Oct 4, 2011 IP
  5. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Ok, it seems there is something missing... something the book never told me to enter...

    MySQLi Error: Table 'pawz_headfirst.mismatch_category' doesn't exist
     
    Matt Ridge, Oct 4, 2011 IP
  6. #6
    There you go :) mismatch_category table is missing...

    "INNER JOIN mismatch_category AS mc USING (category_id) " .
    PHP:
     
    bogi, Oct 4, 2011 IP
  7. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Thats odd. The book never asked for that table to be created. This book seems to miss a lot of things, or puts the cart before the horse a few times.

    Sometimes I wonder if learning this stuff from the book is best, because it seems I seem to debug their code or ask for help to do it more than actually learning from it.
     
    Matt Ridge, Oct 4, 2011 IP
  8. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #8
    No, learning programming from a programming language book is like learning neurosurgery from a French language book. You should learn programming before you learn any programming language (programming is taught in English, not in a programming language), so you'll see the errors as you read the programming language book. Missing a table in a SQL statement is a very basic error.

    Learning a programming language is not learning programming, it's learning a language to do programming in. You have to know programming first. Try Wirth's _Algorithms + Data Structures = Programs_. You can always find a copy on Amazon for a few bucks, and it's the best programming course ever written (I used to use it to teach programming).
     
    Rukbat, Oct 7, 2011 IP