我的数据库中有一个简单的表,其中包含来自用户和来自用户的消息。现在有一个来自用户,另一个来自用户。我想用fromUser组织我的结果,其中包含所有行数据:
Array(
user1 => array(array(
[id] => 1
[message] => some message from user 1
),
(
[id] => 1
[message] => another message from user 1
)),
user2 => array(array(
[id] => 2
[message] => some message from user 2
),
(
[id] => 2
[message] => another message from user 2
)),
user3 => array(array(...
我知道我的语法可能不对,但我希望你明白我的观点。我尝试使用" group by fromName"但那只给了我最后的信息。这样的查询的正确语法是什么?
$query = mysqli_query($link, "SELECT * FROM messages WHERE toName='$to' group by fromName");
$messages = array();
while ($row = mysqli_fetch_assoc($query)) {
$messages[] = $row; //or
$messages[$row['fromName']] = $row; //didn't work either
}
print_r($messages);
修改:刚刚找到了令人毛骨悚然的答案:
$query = mysqli_query($link, "SELECT * FROM messages WHERE toName='$to'");
$messages = array();
while ($row = mysqli_fetch_assoc($query)) {
$messages[$row['fromName']][] = $row;
}
print_r($messages);
猜猜你只需要从&#34; $messages[$row['fromName']->[]<-
&#34;制作数组。