garry Posted May 10, 2008 Share Posted May 10, 2008 Okay so I posted a question on here before and it was answered (thank you very much!) and now I have another problem. For some reason, when i'm trying to display data from the database, the first row (where artist_id is 1) will not show but all others (2,3,4,5..) do show. Can anyone explain why it is not returning the 1st row? Here's the code I'm using: echo "Welcome to the artists page! <br /><br />"; $query =" SELECT * FROM artists "; $result = mysql_query($query); $row = mysql_fetch_array($result); while ($row = mysql_fetch_assoc($result)) { echo "<a href=\"artists.php?id=" . $row['artist_id'] . "\">" . $row['artist'] . "</a><br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/104988-not-showing-first-result-from-database-but-showing-all-after/ Share on other sites More sharing options...
gnawz Posted May 10, 2008 Share Posted May 10, 2008 These could be as a result of other sql queries or variables you have in your code conflicting. Please check. Make sure any include statements are placed rightly. Sometimes, it could be an unnecessary space in your code. Check the database structure esp how you start your numbering. Quote Link to comment https://forums.phpfreaks.com/topic/104988-not-showing-first-result-from-database-but-showing-all-after/#findComment-537392 Share on other sites More sharing options...
Zane Posted May 10, 2008 Share Posted May 10, 2008 $row = mysql_fetch_array($result); while ($row = mysql_fetch_assoc($result)) { Because you've called mysql_fetch_array twice....well not neccessarily fetch array but the mysql_fetch Class and whenever you do that it moves onto the next row...that's why the loop works like that. From the PHP manual http://php.net/mysql_fetch_array Description array mysql_fetch_array ( resource $result [, int $result_type ] ) Returns an array that corresponds to the fetched row and moves the internal data pointer ahead. Quote Link to comment https://forums.phpfreaks.com/topic/104988-not-showing-first-result-from-database-but-showing-all-after/#findComment-537399 Share on other sites More sharing options...
garry Posted May 10, 2008 Author Share Posted May 10, 2008 So what do I need to do in order to fix this? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/104988-not-showing-first-result-from-database-but-showing-all-after/#findComment-537402 Share on other sites More sharing options...
Zane Posted May 10, 2008 Share Posted May 10, 2008 Think long and hard...... Because you've called mysql_fetch_array twice Quote Link to comment https://forums.phpfreaks.com/topic/104988-not-showing-first-result-from-database-but-showing-all-after/#findComment-537407 Share on other sites More sharing options...
garry Posted May 10, 2008 Author Share Posted May 10, 2008 Yes but i'm not sure how to remove one of them and retain the row variable. I'm very new to php and still learning - please remember that we all aren't gurus Quote Link to comment https://forums.phpfreaks.com/topic/104988-not-showing-first-result-from-database-but-showing-all-after/#findComment-537412 Share on other sites More sharing options...
Zane Posted May 10, 2008 Share Posted May 10, 2008 $row = mysql_fetch_array($result); while ($row = mysql_fetch_assoc($result)) { That said line is telling the database to skip a row basically. Notice the similarities in the red areas. You started looping the rows of the db after you told it to skip a line EDIT:I know were not all gurus, I'm just trying to show you whats wrong instead of doing it for you. That's how you learn Quote Link to comment https://forums.phpfreaks.com/topic/104988-not-showing-first-result-from-database-but-showing-all-after/#findComment-537418 Share on other sites More sharing options...
garry Posted May 10, 2008 Author Share Posted May 10, 2008 Ah that worked, thank you. I only started learning PHP a week ago and am still trying to understand how it all works. Thanks for your help EDIT:I know were not all gurus, I'm just trying to show you whats wrong instead of doing it for you. That's how you learn Yeah, I know. It was just that I was thinking that the row variable was only being set by that line and that deleting it would produce an error. Thanks for showing me. Quote Link to comment https://forums.phpfreaks.com/topic/104988-not-showing-first-result-from-database-but-showing-all-after/#findComment-537422 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.