user1149499
user1149499

Reputation: 583

Pulling mysql data into a multidimensional array

I've checked all kinds of previously answered similar questions and nothing's working. I have a "Inbox" type application where a person of [email protected] might have 5 messages waiting for them. They log in and get a screen of those five messages, the sender email, the date sent, and the first 15 chars of the message.

In mysql, I have: id, emailaddress, senderemail, datesent, fullmessage.

I'm using SELECT * FROM db WHERE emailaddress='[email protected]' to pull out, let's say, 5 new messages. $result equals the mysql query. mysql_num_rows does a great job of telling me there are 5 rows. It works fine up to that point.

My problem is that I can't (despite hours of trying) find the right syntax for making this into a multidimensional array so that I can iterate table rows in a for loop and print it all out. I'm envisioning:

<table>
for ($a = 0; $a < $numrows; $a++) {
<tr>
     the php code would go here and would print the associated sender, date, and message in <td> cells.
</tr>
}
</table>

Any help would be fantastic.

I'm sure this is not hard but have just not gotten anywhere for most of the afternoon. Can anyone out there tell me what a simple syntax for doing this would be?

Upvotes: 0

Views: 2491

Answers (1)

Biotox
Biotox

Reputation: 1601

Information that comes from the database is in an array when you use mysql_fetch_array() or mysql_fetch_assoc(). So, you can do something like:

<table>
<?php
$storage_array = array();
$result = mysql_query("SELECT * FROM db WHERE emailaddress='[email protected]'" or die(mysql_error());

while($data = mysql_fetch_assoc($result))
{
?>
<tr>
  <td><?php echo $data['sender'] ?></td>
  <td><?php echo $data['date'] ?></td>
  <td><?php echo $data['message '] ?></td>
</tr>
<?php
  $storage_array[] = $data;
}
?>
</table>

You will obviously need to join some tables for messages if they are on different tabels or use LIMIT to only get 5 rows. You will also need to determine your ORDER BY.

You will need to know which loop added your data to $storage_array and remember it starts with $storage_array[0].

Upvotes: 3

Related Questions