mysql query + arrays + while loops returns 1 row too little?

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
snek_one
Forum Newbie
Posts: 7
Joined: Wed May 21, 2003 9:07 am

mysql query + arrays + while loops returns 1 row too little?

Post by snek_one »

I don't know if this is a common problem or not, but for some weird reason i get one row too little as output. there are 3 rows with id's 1,2,3 but it outputs only 3,2 in that order. I did sort by another variable though, so that's why they come out like that. But why the first one is missing...

Code is structured like this:

Code: Select all

while query{
   echo some stuff
   while query{
      while query using previous returned id's{
      echo some stuff
      }
   }
}
now the weird part is that i echo'd back some rowcounts of the queries and it outputs 3 the first time, then 2 the second time:

Code: Select all

<?php
	//SELECT informatie over product
	mysql_select_db($database_neoderma_nl, $neoderma_nl);
	$query_producten = "SELECT id, naam, werking_nl, plaatje, werking_en, werking_fr FROM product ORDER BY naam ASC";
	$producten = mysql_query($query_producten, $neoderma_nl) or die(mysql_error());
	$row_producten = mysql_fetch_array($producten);
	$totalRows_producten = mysql_num_rows($producten);
	echo $totalRows_producten; ///////////////////OUTPUTS 3!!
?>
<table width="400" border="0" cellpadding="0" cellspacing="0">
  <?php
		while($row_producten=mysql_fetch_array($producten))&#123;
				$productID = $row_producten&#1111;0];
				echo $productID; ///////////////////OUTPUTS 2!!
				$productNaam = $row_producten&#1111;1];
				$productWerking = $row_producten&#1111;2];
			?>
The product table which gives me the 2 values has 3 rows with 3 null collumns in EACH row.. So there shouldn't be any difference, should there? I read something somewhere about null values using mysql_fetch_array, could that be the problem?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

This line:

Code: Select all

$row_producten = mysql_fetch_array($producten);
is calling the first result of the query so that the while loop starts only with the second. Delete the line above and that should have all of the records being retrieved in the while loop.

Mac
Post Reply