0

Possible Duplicate:
Looping through mysql_fetch_array in PHP

I have a simple join query that should echo out each array result in a foreach loop. The array is not empty. Printing the array count works -- there are 801 results. However, actually printing out the rows either above or within the foreach only prints the first result. Any help would be appreciated.

$listQuery = "SELECT r.email_address
            FROM wholesale_accounts a
            LEFT JOIN wholesale_register r ON r.register_id = a.register_id
            WHERE r.email_address != ''
            ORDER BY r.email_address";            
$listResult = mysql_query($listQuery);
//print_r($listResult); // prints (Resource ID #21)
//exit;

$rows = mysql_fetch_array($listResult, MYSQL_ASSOC);
$count = mysql_num_rows($listResult);
//print_r($count); // prints 801
print_r($rows); // only prints first email address
exit;

foreach($rows as $row) {
    print_r($row); // prints email address          
}
3
  • It is better for you to pick a good book or tutorial on any language of your choice than to post questions like this here. It shows you've not been paying attention to what you read or you didn't take the pain to understand how things work. Technology is moving fast and this one is old - embrace PDO or MySQLi Commented Oct 1, 2012 at 22:07
  • Thanks @codingbiz. It sounds like you need some more information. I am attempting to fix a broken piece of old site that I did not build. That's my main job description. If I had built this site, it would be PDO. I have debugged this many ways -- that's why I am posting here. But thanks for your opinion anyway. Commented Oct 2, 2012 at 2:20
  • I was being honest with you mate. I learned that truth when I joined SO few months ago - we need to be able defend what we know so others can learn from us and trust our judgment. Hope you got the problem solved? :) Commented Oct 2, 2012 at 6:11

1 Answer 1

3

mysql_fetch_array gets one row at a time so you need to put that in a loop

while($rows = mysql_fetch_array($listResult, MYSQL_ASSOC))
{
   print_r($rows);
}
Sign up to request clarification or add additional context in comments.

1 Comment

A while loop didn't quite get it, but using list() with it worked. Here's what I ended up with: while (list($email_address) = mysql_fetch_row($listResult)) { echo $email_address . '<br>'; }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.