echoing 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
dlusiondone
Forum Newbie
Posts: 4
Joined: Fri May 22, 2009 12:55 am

echoing an array

Post by dlusiondone »

Hi guys,

My tables looks like this:

movieID movie_title
1 Seven Pounds
2 Tranformers
3 Angels & Demons
4 Bamboozelled
5 Waltz With Bashir
dlusiondone
Forum Newbie
Posts: 4
Joined: Fri May 22, 2009 12:55 am

Re: echoing an array

Post by dlusiondone »

Hi guys,

My tables looks like this:

movieID movie_title
1 Seven Pounds
2 Tranformers
3 Angels & Demons
4 Bamboozelled
5 Waltz With Bashir

And my code looks like this:

<?php

while($row = mysql_fetch_array($result))
{
echo $row['movieID']. " - " .$row['movie_title'];
echo "<br />" ;
}

?>

But for some reason it only returns this:

2 - Tranformers
3 - Angels & Demons
4 - Bamboozelled
5 - Waltz With Bashir

As you can see the first table entry is missing! Any suggestions?

Thanks in advance.
User avatar
Gabriel
Forum Commoner
Posts: 41
Joined: Wed May 06, 2009 8:12 pm
Location: Las Vegas

Re: echoing an array

Post by Gabriel »

Welcome to the forums. Please use the proper [ code=php ] ... [ /code ] tags.
 
What kind of SQL query are you using?
 

Code: Select all

SELECT * FROM movies -- Anything like this?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: echoing an array

Post by jaoudestudios »

Whats the code before, just as Gabriel said how is your query? I have seen this problem many times before when mysql_fetch_assoc is used twice on the same resource.
dlusiondone
Forum Newbie
Posts: 4
Joined: Fri May 22, 2009 12:55 am

Re: echoing an array

Post by dlusiondone »

Oh yeah, sorry guys! Im assuming what Gabriel was referring to was that the code i post, needs to be placed between [ code=php ] & [ /code ] so that it looks like Gabriel's post?????

Code: Select all

 
<?php
 
include ('server.php');
 
$query = "select * from movies";
 
$result = mysql_query($query) or die(mysql_error());
 
$row = mysql_fetch_array($result) or die(mysql_error());
 
?>
 
Last edited by Benjamin on Fri May 22, 2009 9:45 am, edited 1 time in total.
Reason: Added [code=php] tags.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: echoing an array

Post by Mark Baker »

What is the purpose of this line?
$row = mysql_fetch_array($result) or die(mysql_error());
You're fetching and discarding the first row returned from the database before going into your while loop
dlusiondone
Forum Newbie
Posts: 4
Joined: Fri May 22, 2009 12:55 am

Re: echoing an array

Post by dlusiondone »

Legend! Thanks Mark! Much appreciated!
Post Reply