title: How to load MySQLi result set into two-dimensional array?
body: <p>I've got a problem with the mysqli result set. I have a table that contains a bunch of messages. Every table row represents one message. I have a few columns like ID, title, body, and 'public'. The public column contains booleans, that specify if the message is to be displayed to everyone, or just to the person who posted it. I have a page where I want to display all public messages, and if you click on a message, you get a page with the single message, and some extra options. To do this, I want to load the result of a mysqli query into a two dimensional array. That would mean an array of messages, and each message is an array on itself with the ID, title, body etc.. as columns. </p>
  
  <p>So I started out with the following code. The '$link' variable contains the mysqli connection (witch works fine).</p>
  
  <pre><code>$result = $link-&gt;query("SELECT * FROM messages WHERE public = '1'");
  $array = $result-&gt;fetch_assoc();
  
  print_r($array);
  </code></pre>
  
  <p>This only results in a one-dimensional array, with the latest message in it. So I tried the following while loop:</p>
  
  <pre><code>$result = $link-&gt;query("SELECT * FROM messages WHERE public = '1'");
  
  while($message = $result-&gt;fetch_assoc()){
   $title = $message['title'];
   $body = $message['body'];
   # etc... 
  }
  </code></pre>
  
  <p>This works in a way: It displays all messages, but does not put them in an array (witch I want for performing tasks based on ID, and the position of the message array in the containing array.) Does anybody know how to convert this kind of query result into a nice two-dimensional array? Or a totally different, nifty way to go about this? Thanks in advance.</p>
  
  <p>PS. Sorry for my English, i'm not a native speaker.</p>