PHP Noob here - trouble displaying last value in an array

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
molossus
Forum Newbie
Posts: 1
Joined: Mon Sep 27, 2010 2:12 pm

PHP Noob here - trouble displaying last value in an array

Post by molossus »

Hey, as I said, I am working on my very first project in php. So far so good! However, there is something I can't get past. I am sure it's simple, I am just missing it.

The problem is that, while my data fills out the table very nicely, it does not include the last row. If there are 3 entries, it will only show 4, etc.

Here's my code:

Code: Select all

$row = mysql_fetch_array($result);
$rowcolor = 1;

while($row = mysql_fetch_array($result))
{
    if ( $rowcolor==2 ) { echo "<tr class='rowColorDark'><td>"; $rowcolor = 0; } else { echo "<tr class='rowColorLight'><td>"; }
    echo $row['distroname'] . "</td><td>" . $row['logins'] . "</td><td>" . $row['lastlogin'] . "</td><td>" . $row['username'] . "</td><td>" . $row['password'] . "</td></tr>";
    $rowcolor++;
} 
I'm a little, tiny baby.
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Re: PHP Noob here - trouble displaying last value in an arra

Post by jabbaonthedais »

You mean if there are 4 entries it only shows 3? You said that backwards. There could be something in your query excluding the extra row. Have you tried running the query in phpmyadmin to see if it brings back all the rows?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Noob here - trouble displaying last value in an arra

Post by AbraCadaver »

Because you do mysql_fetch_array() for the first row and you don't do anything with it. Then you start your loop and it starts at the second record. Get rid of the:

Code: Select all

$row = mysql_fetch_array($result);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply