Jump to content

Recommended Posts

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 />";
	}

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.

$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.

$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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.