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

后端 未结 3 886
囚心锁ツ
囚心锁ツ 2021-01-13 08:54

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

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-13 09:13

    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.

提交回复
热议问题