简体   繁体   中英

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

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.

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);

If i get you right, you want to achieve something that is produced by:

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

$messages = array();
while($singleMessage = $result->fetch_assoc()){
  $messages[$singleMessage]['title'] = $singleMessage['title'];
  $messages[$singleMessage]['body'] = $singleMessage['body'];
}

This will get you an 2 dimensional array, using the ID as key.

How your final array must look like?

Try this:

$result = $link->query("SELECT title, body FROM messages WHERE public = '1'");
$array = array();
while ($array[] = mysql_fetch_assoc($result)) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM