1

I have been writing some code which pulls entries from a MySQL database. These are numbers 1 to 38

However, it only returns every second number i.e. 2,4,6,8 instead of 1,2,3,4.

$result = mysql_query("select (caseID) FROM `case` order by caseID")
 or die(mysql_error());  

while(mysql_fetch_array( $result )) 
{ 
    $row = mysql_fetch_assoc($result); 
    $countName= $row['caseID'];

    Print $countName;
} 

I've tried various changes and reducing the code to the bare minimum. But nothing seems to work.

1
  • Heads up! The next major release of PHP is deprecating the mysql_ family of functions. Now would be a great time to switch to PDO or mysqli. Commented Jan 7, 2013 at 10:06

2 Answers 2

6

Calling mysql_fetch_array two times, thats why.

Try this

while($row=mysql_fetch_assoc( $result )) 
{ 
$countName= $row['caseID'];
 print $countName;
} 
Sign up to request clarification or add additional context in comments.

1 Comment

Huh? What do you mean by that?
0

It is because you are calling mysql_fetch_array, which retrieves one result, and then you call mysql_fetch_assoc, which retrieves yet another result. That is then repeated until there are no more results left.

Or, in other words, you are fetching one result without using it, and then fetching another result which is then used, effectively jumping over every other result.

This should do the job:

while($row = mysql_fetch_assoc($result)) 
{ 
    print $row['caseID'];
}

Also, take a look at the documentation. Turn your eyes to the big box that says "Warning" and has a stop sign in it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.