如何将MySQLi结果集加载到二维数组中? [英] How to load MySQLi result set into two-dimensional array?

查看:125
本文介绍了如何将MySQLi结果集加载到二维数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的mysqli结果集有问题.我有一个包含一堆消息的表.每个表格行代表一条消息.我有一些列,例如ID,标题,正文和"public". public列包含布尔值,用于指定是向所有人显示消息,还是向发布者显示消息.我有一个页面要显示所有公共消息,如果单击某条消息,则会显示一个页面,其中包含单个消息以及一些其他选项.为此,我想将mysqli查询的结果加载到二维数组中.那将意味着一个消息数组,每个消息本身就是一个数组,以ID,标题,正文等作为列.

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.

所以我从下面的代码开始. "$ link"变量包含mysqli连接(女巫工作正常).

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

这只会产生一维数组,其中包含最新消息.所以我尝试了以下while循环:

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... 
}

这以某种方式起作用:它显示所有消息,但不将它们放在数组中(我要根据ID执行任务的女巫,以及消息数组在包含数组中的位置.)有人知道如何做吗?将这种查询结果转换为漂亮的二维数组?还是完全不同的解决方法?预先感谢.

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.对不起,我的英语不是我的母语.

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

这将导致如下结果:

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

如果您希望将ID作为密钥,请执行以下操作:

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

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

这将导致:

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

在PHP 5.3中,您还获得了一个新方法,该方法与我发布的第一个代码示例相同:

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

这篇关于如何将MySQLi结果集加载到二维数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆