I have a webpage that builds an html table if there is any data in a mySQL table. That works fine, the html table is populated with data from the mySQL table. My problem is when there is data it populates the html table but lists the first mySQL table entry twice in the html table. I do not have any idea why this happens.

Here's the relevant code:

$PNDG_USRS_S1 = mysql_query("SELECT * FROM `sql_table`");
$PNDG_USRS = mysql_fetch_assoc($PNDG_USRS_S1);
if (mysql_num_rows($PNDG_USRS_S1 = mysql_query("SELECT * FROM `sql_table`")) > 0) {
?>
<table id="table">
<tr>
<td class='block_title'>
Name
</td>
<td class='block_title'>
Email
</td>
<td class='block_title'>
Grade
</td>
<td class='block_title'>
Gender
</td>
<td class='block_title'>
Birthday
</td>
<td class='block_title'>
Accept?
</td>
</tr>
<?php
 do { 
 ?>
 <tr>
 <td>
 <?php echo $PNDG_USRS['Name']; ?>
 </td>
 <td>
 <?php echo $PNDG_USRS['email']; ?>
 </td>
 <td>
 <?php echo $PNDG_USRS['grade']; ?>
 </td>
 <td>
 <?php echo $PNDG_USRS['Gender']; ?>
 </td>
 <td>
 <?php if ($PNDG_USRS['birthdate'] == "0000-00-00") {echo "N/A";}else{echo $PNDG_USRS['birthdate'];}; ?>
 </td>
 <td>
 <a href='<?php echo $_SESSION['RP'], "/admin/?state=USR_VRFY&usr_id=", $PNDG_USRS['ID']; ?>' onclick="return confirm('Are you sure you want to verify the user <?php echo $PNDG_USRS['Name']; ?> and make them an active user?');">Verify User?</a>
 </td>
 </tr>
 <?php
 } while ($PNDG_USRS = mysql_fetch_assoc($PNDG_USRS_S1));
?>
 </table>
 <?php
} else {
?>
<center>
<div id='MSG'>
No users pending approval
</div>
</center>
<?php
};
};
};

The page is designed to list all new users on a website. An administrator or moderator must confirm each user before they have access, that's what this page will be used for.

Thanks in advanced! The new school year is starting and we will have a large number of new users soon so I'd like to have this form working soon. Again, thanks!

Recommended Answers

All 2 Replies

Member Avatar for diafol
$PNDG_USRS_S1 = mysql_query("SELECT * FROM `sql_table`");
$PNDG_USRS = mysql_fetch_assoc($PNDG_USRS_S1);
if (mysql_num_rows($PNDG_USRS_S1 = mysql_query("SELECT * FROM `sql_table`")) > 0) {

That makes little sense to me.

We usually avoid a 'do-while' loop, opting for a standard 'while' instead.

Only make one query.

1. Make the query
2. check number of rows (your syntax is wrong I think)
3. Add html table and headers if more than 0 rows
4. Loop using standard 'while', adding rows to the html table
5. Add html table end if rows more than 0 rows

That should make life a lot easier for you.

commented: Did not seem to full understand posted code. +1
$PNDG_USRS_S1 = mysql_query("SELECT * FROM `sql_table`");
$PNDG_USRS = mysql_fetch_assoc($PNDG_USRS_S1);
if (mysql_num_rows($PNDG_USRS_S1 = mysql_query("SELECT * FROM `sql_table`")) > 0) {

That makes little sense to me.

We usually avoid a 'do-while' loop, opting for a standard 'while' instead.

Only make one query.

1. Make the query
2. check number of rows (your syntax is wrong I think)
3. Add html table and headers if more than 0 rows
4. Loop using standard 'while', adding rows to the html table
5. Add html table end if rows more than 0 rows

That should make life a lot easier for you.

I see where your going with it. I changed the Do while statement to while like you suggested and re wrote the mysql_ statement. I'm still not sure what was wrong with it, however it does work now. Thank you.

For reference of anyone else with a similar problem(no matter if I just made a stupid mistake or not), here's the code I changed.

The updated mySQL calls:

$PNDG_USRS_S1 = mysql_query("SELECT * FROM `sql_table`");
$PNDG_USRS_S2 = mysql_num_rows($PNDG_USRS_S1);
if ($PNDG_USRS_S2 > 0) {

and the new while function:

while ($PNDG_USRS = mysql_fetch_assoc($PNDG_USRS_S1)):
 ?>
 <tr>
 <td>
 <?php echo $PNDG_USRS['Name']; ?>
 </td>
 <td>
 <?php echo $PNDG_USRS['email']; ?>
 </td>
 <td>
 <?php echo $PNDG_USRS['grade']; ?>
 </td>
 <td>
 <?php echo $PNDG_USRS['Gender']; ?>
 </td>
 <td>
 <?php if ($PNDG_USRS['birthdate'] == "0000-00-00") {echo "N/A";}else{echo $PNDG_USRS['birthdate'];}; ?>
 </td>
 <td>
 <a href='<?php echo $_SESSION['RP'], "/admin/?state=USR_VRFY&usr_id=", $PNDG_USRS['ID']; ?>' onclick="return confirm('Are you sure you want to verify the user <?php echo $PNDG_USRS['Name']; ?> and make them an active user?');">Verify User?</a>
 </td>
 </tr>
 <?php
endwhile;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.