duanci1858
2014-09-13 05:57
浏览 124
已采纳

如何将多行MySQL数据库放入多维数组(PHP)?

I am working on a system where a query checks the database for records matching multiple ids and puts each row into it's own array. So far I only got the last row to echo out in it's own array. Here is the query code:

$query = "SELECT * FROM messages WHERE senderid IN ($senderids) ORDER BY messageid DESC";
$resource = mysql_query($query, $database);
$result = mysql_fetch_array($resource);
foreach($result as $result1)
    {
    print_r($result1);
    echo '<br>';
    }

This is what echos in the browser:

2
2
1
1
TEST MSG 2
TEST MSG 2
2014_09_13_01:29:59
2014_09_13_01:29:59

This is what should echo:

Array ([messageid] => 1 [senderid] => 1 [message] => test message [date] => 2014_09_13_01:01:09)
Array ([messageid] => 2 [senderid] => 1 [message] => TEST MSG 2 [date] => 2014_09_13_01:29:59)

How would I go about fixing this issue?

图片转代码服务由CSDN问答提供 功能建议

我正在研究一个系统,在该系统中,查询检查数据库中是否有匹配多个ID的记录,并将每行放入其中 阵列。 到目前为止,我只有最后一行在它自己的数组中回显。 这是查询代码:

  $ query =“SELECT * FROM messages WHERE senderid IN($ senderids)ORDER BY messageid DESC”; 
 $ resource = mysql_query($ query  ,$ database); 
 $ result = mysql_fetch_array($ resource); 
foreach($ result as $ result1)
 {
 print_r($ result1); 
 echo'&lt; br&gt;'; 
} \  n   
 
 

这就是浏览器中的回声:

  2 
2 
1 
1 
TEST MSG 2 \  nTEST MSG 2 
2014_09_13_01:29:59 
2014_09_13_01:29:59 
   
 
 

这应该回应:

 <  code> Array([messageid] =&gt; 1 [senderid] =&gt; 1 [message] =&gt; test message [date] =&gt; 2014_09_13_01:01:09)
Array([messageid] =&gt; 2 [senderid  ] =&gt; 1 [消息] =&gt;测试MSG 2 [日期] =&gt; 2014_09_13_01:29:59)
   
 
 

我将如何解决此问题 问题?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题