简体   繁体   English

如何将MySQLi结果集加载到二维数组中?

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

I've got a problem with the mysqli result set. 我遇到了mysqli结果集的问题。 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'. 我有一些列,如ID,标题,正文和'公共'。 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. 为此,我想将mysqli查询的结果加载到二维数组中。 That would mean an array of messages, and each message is an array on itself with the ID, title, body etc.. as columns. 这意味着一组消息,每条消息都是一个数组本身,ID,标题,正文等作为列。

So I started out with the following code. 所以我开始使用以下代码。 The '$link' variable contains the mysqli connection (witch works fine). '$ link'变量包含mysqli连接(女巫工作正常)。

$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: 所以我尝试了以下while循环:

$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? 这在某种程度上起作用:它显示所有消息,但不将它们放在一个数组中(我想要根据ID执行任务,以及消息数组在包含数组中的位置。)有人知道如何转换这个那种查询结果变成了一个漂亮的二维数组? 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;
}

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: 如果您想将ID作为键,请执行以下操作:

$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: 在PHP 5.3中,您还获得了一个新方法,它与我发布的第一个代码示例相同:

$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. 这将使用ID作为键来获得二维数组。

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)) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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