[SOLVED] Simple loop through an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

[SOLVED] Simple loop through an array

Post by charp »

Perhaps I'm just tired :?, but I cannot figure out what I'm doing wrong here:

Code: Select all

$results=mysql_query("SELECT post_id FROM $table");
$row=mysql_fetch_array($results);
while ($row=mysql_fetch_array($results)) { 
$entry=$row["post_id"];
echo $entry.'<br>';
}
All it's supposed to do is list all the 'post_id' values from the table. But for some reason, it always starts on the second entry and skips the first. The results are the same even if I change to a different column in the table.

Thanks in advance.
Last edited by charp on Sat Jul 23, 2005 10:13 pm, edited 1 time in total.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Re: Simple loop through an array

Post by nielsene »

charp wrote:Perhaps I'm just tired :?, but I cannot figure out what I'm doing wrong here:

Code: Select all

$results=mysql_query("SELECT post_id FROM $table");
$row=mysql_fetch_array($results);
while ($row=mysql_fetch_array($results)) { 
$entry=$row["post_id"];
echo $entry.'<br>';
}
All it's supposed to do is list all the 'post_id' values from the table. But for some reason, it always starts on the second entry and skips the first. The results are the same even if I change to a different column in the table.

Thanks in advance.
Remove line 2.
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

Post by charp »

Thanks nielsene, that worked.

Even after I had it working it took me a couple of moments to see what I was doing wrong! Two instance of mysql_fetch_array-- Duh :oops:
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

yeah, its always those simple things that you can't find in your own code.... The hours I've wasted with similiar type bugs.....
Post Reply