How to load MySQLi result set into two-dimensional array?

16,675

You're almost there, you would only need to change a few things:

$result = $link->query("SELECT * FROM messages WHERE public = '1'");
$messages = array();
while($message = $result->fetch_assoc()){
   $messages[] = $message;
}

This would result in something like this:

array(
  0 => array('message' => ..., 'subject' => ...), 
  1 => array('message' => ..., 'subject' => ...), 
  2 => array('message' => ..., 'subject' => ...), 
);

If you want the IDs as the keys, do something like this:

$messages = array();
while($message = $result->fetch_assoc()){
   $messages[ $message["id"] ] = $message;
}

Which would result in:

array(
  123 => array('message' => ..., 'subject' => ...), 
  456 => array('message' => ..., 'subject' => ...), 
  789 => array('message' => ..., 'subject' => ...), 
);

In PHP 5.3, you also get a new method, which does the same as the first code example I posted:

$messages = $result->fetch_all(MYSQLI_ASSOC);
Share:
16,675

Related videos on Youtube

Stefan Hagen
Author by

Stefan Hagen

Ruby Rails PostgreSQL (& SQL) Javascript/Jquery/Coffeescript HTML/CSS & HAML/SASS Interaction Design User Experience Design

Updated on June 04, 2022

Comments

  • Stefan Hagen
    Stefan Hagen almost 2 years

    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.

    So I started out with the following code. The '$link' variable contains the mysqli connection (witch works fine).

    $result = $link->query("SELECT * FROM messages WHERE public = '1'");
    $array = $result->fetch_assoc();
    
    print_r($array);
    

    This only results in a one-dimensional array, with the latest message in it. So I tried the following while loop:

    $result = $link->query("SELECT * FROM messages WHERE public = '1'");
    
    while($message = $result->fetch_assoc()){
     $title = $message['title'];
     $body = $message['body'];
     # etc... 
    }
    

    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.

    PS. Sorry for my English, i'm not a native speaker.

  • RickN
    RickN over 12 years
    I think you'll need to change [$singleMessage] to [$singleMessage['id']], otherwise, PHP would use the entire array as a key... (Which might actually work!)