title: Getting multiple rows from a mysql query with prepared statements?
body: <p>I was going through some <a href="http://mattbango.com/notebook/code/prepared-statements-in-php-and-mysqli/" rel="nofollow">tutorials</a> on prepared statements, and I couldn't figure out how to loop through results that contain multiple rows. I am used to the procedural style of</p>
  
  <pre><code>while($row = mysqli_fetch_array($result))
  </code></pre>
  
  <p>However I cannot for the life of me figure out how to do this with the prepared statement format. </p>
  
  <pre><code>if($stmt = $mysqli-&gt;prepare("SELECT * FROM `Products` WHERE name LIKE '%?%'")){
  
          $stmt-&gt;bind_param("s", $query); //query is $_GET['query'], user input
  
          $stmt-&gt;execute();
          $result = null;
          $stmt-&gt;bind_result($result);
  
          while($stmt-&gt;fetch()){ //problematic code...
              echo $result;
          }
  
          $stmt-&gt;close();
  }
  </code></pre>
  
  <p>How can I loop through multiple rows resulting from a prepared statement SQL query in PHP?</p>
  
  <p>EDIT (new code as requested):</p>
  
  <pre><code>/* Create a prepared statement */
      if($stmt = $mysqli-&gt;prepare("SELECT name, price, description, file FROM `Products` WHERE name LIKE '%?%'")){
  
          $stmt-&gt;bind_param("s", $query) or die('bind_param');
          $query = trim(htmlspecialchars($_GET['query']));
          $stmt-&gt;execute() or die('execute');
          $stmt-&gt;bind_result($name, $price, $description, $file) or die('bind_result');
  
  
          while($stmt-&gt;fetch()){
              echo $name;
          }
  
          $stmt-&gt;close();
      }
  </code></pre>