Artur Lipiński
Artur Lipiński

Reputation: 69

mysql_fetch_array and while loop lost first result

I have problem with mysql_fetch_array() and while loop. I have the query:

$tagsquery = mysql_query("SELECT `url` FROM `tags`, `mapa-tagow`, `statusy` WHERE `tags`.`id` = `mapa-tagow`.`tag-id` AND `statusy`.`id` = `mapa-tagow`.`article-id` AND `tags`.`tag` ='$tag' ORDER BY `url` ASC ") or die("ERROR: Tags doesn't exist."); 

And results in while loop:

while($tags = mysql_fetch_array($tagsquery)) {
   echo "<a href='tags.php?url=$url'>$url</a>, ";
}   

When testing this query in PHPMyAdmin I have one more result than I get in PHP. I dont know why PHP always missing first result.

Upvotes: 1

Views: 1039

Answers (2)

Kenaniah
Kenaniah

Reputation: 5201

A few possibilities:

  • You're looking at a different copy of the same database
  • You've already started fetching your result set earlier than your while loop

On a side note, you should build an array of your tag links, and then output them using implode... while(){ $tag_links[] = '<a href...';} print implode(', ', $tag_links);

Upvotes: 3

Mathieu Dumoulin
Mathieu Dumoulin

Reputation: 12244

You need to change your code to:

while($tags  = mysql_fetch_array($tagsquery)) {
    echo '<a href="tags.php?url='.$tags['url'].'">'.$tags['url'].'</a>, ';
}

Note that i took the opportunity to correct your tag output, there are many other ways to output HTML but this is easier to read in most cases.

If you can't spot the problem, i changed your $url to $tags['url'], so i output the URL from the $tags that you got from mysql_fetch_array()

Cheers

Upvotes: 1

Related Questions