title: Unable to display result of MySQLi SELECT query
body: <p>This is my first attempt at using PHP with a Database (MySQLi).  I finally got the database connecting (at least, I am not getting any errors).</p>
  
  <p>Now I trying to get the result of a select query and display it in my form, but I cannot get anything to display.</p>
  
  <p>Where am I going wrong?</p>
  
  <p>This is the code to retrieve the query results from the database:</p>
  
  <pre><code>/*Connect To DB*/
  $conn = mysqli_connect($host, $user, $pwd)
          or die("Could not connect: " . mysql_error()); //connect to server
      mysqli_select_db($conn, $database)
          or die("Error: Could not connect to the database: " . mysql_error());
  
      /*Check for Connection*/
      if(mysqli_connect_errno()){
          /*Display Error message if fails*/
          echo 'Error, could not connect to the database please try again later.';
      exit();
      }
  /*Query for states*/
  $query = "SELECT StateAbbreviation, StateName FROM USState ORDER BY StateName";
  $result = mysqli_query($conn, $query);
  $num_results = mysqli_num_rows($result);
  ?&gt;
  </code></pre>
  
  <p>This is the form where the results should display:</p>
  
  <pre><code>&lt;form id="StateSelector" action="" method="post"&gt; 
  &lt;select size="1" name="states" id="states"&gt; 
  &lt;option value ""&gt;--Select State--&lt;/option&gt;
  
  &lt;!--Loops through the states--&gt; 
  
  
   &lt;?
    /*Loop through through each stat and display as an option as a drop-down field */
  
  
    for($i=0; $i&lt;$num_results; $i++) {
        $row = mysqli_fetch_assoc($result);
        echo 'option value="' .$row['StateAbbreviation'] . '"&gt;' . $row['StateName'] . '&lt;/option&gt;' . "\n";
    }
  ?&gt;
    &lt;/select&gt;
  
  &amp;nbsp; Zip:
  &lt;input type="text" name="zip" size="5" /&gt;&lt;/p&gt;
  &lt;/form&gt;
  
  &lt;p&gt;Your email address:&lt;br/&gt;
  &lt;input type="text" name="email" size="20" /&gt;&lt;/p&gt;
  
  &lt;p&gt;Please let us know what you think:&lt;br/&gt;
  &lt;textarea name="feedback" rows="12" cols="40" wrap="virtual" /&gt;&lt;/textarea&gt;&lt;/p&gt;
  
  &lt;p&gt;&lt;input type="submit" value="Send feedback" /&gt;&lt;/p&gt;
  
  &lt;/form&gt;
  </code></pre>