title: Getting mysqli result as array for json_encode() in PHP
body: <p>I finally convinced myself to switch from PHP mysql to mysqli after upgrading my old PHP version. However, I did not manage to implement the same approach as before:</p>
  
  <p>This is the old approach:</p>
  
  <pre><code>$sth = mysql_query("select * from .....");
  
  $rows = array();
  
  while($r = mysqli_fetch_assoc($sth)) {
      $rows[] = $r;
  }
  
  print json_encode($rows);
  </code></pre>
  
  <p>This is my mysqli approach:</p>
  
  <pre><code>$prename = "Peter";
  
  $rows = array();
  
  $mysqli = new mysqli($server, $user, $pass, $dbase);
  
  if ($stmt = $mysqli-&gt;prepare("select lastname where prename = ? order by prename asc")) {
  
      /* bind parameters for markers */
      $stmt -&gt; bind_param("s", $prename);
  
      /* execute query */
      $stmt -&gt; execute();
  
      /* bind result variables */
      $stmt -&gt; bind_result($lastname);
  
      /* fetch value */
      $stmt-&gt;fetch();
  
      echo $lastname;
  
      /* close statement */
      $stmt -&gt; close();
  }
  
  /* close connection */
  $mysqli -&gt; close();
  
  print json_encode($rows);
  </code></pre>
  
  <p>How can I add the result of the query to the <code>$rows[]</code> array? The return value has to be a json string that will be parsed by my webapplication. I tried several solutions with <code>$stmt -&gt; fetch_array</code> but none of them worked.</p>
  
  <p>Thank you very much for your help.</p>