ArmyAngel ArmyAngel - 1 year ago 78
MySQL Question

First line of PHP while loop doesn't echo to screen

In the following section of code. I'm checking my

and output one of two things. This works fine. The problem is in the while loop. It doesn't echo out the first record.

// Get existing emails in system
$email_data = mysql_query("SELECT * FROM `massmail`");
$row2 =mysql_fetch_array($email_data);
$email_count = mysql_num_rows($email_data);

// Get the campaign name for existing emails
$latest_campaign = $row2['campaign_id'];
$latest_data = mysql_query("SELECT * FROM campaign WHERE `id`='$latest_campaign'");
$latest_row = mysql_fetch_array($latest_data);
$latest_name= $latest_row['name'];

// Display Options depending on availability of emails,
if($email_count ==0){
echo "upload new file";

echo "<span class=\"text_1\">The following <strong>".$email_count."</strong> emails are in the system for <strong>".$latest_name."</strong> campaign.<br><br>";
echo "<div class=\"rounded-corners1\" id=\"latest_emails\" align=\"left\" style=\"overflow: auto; height: 200px; width:250px; \" >";


while($row2 = mysql_fetch_array($email_data)){
echo $row2['email'].'<br/>';
echo "<br/></span></div><br/>";
<table width="600" border="0" cellspacing="10" cellpadding="10">
<td width="50%" align="center" valign="top" bgcolor="#336699" class="rounded-corners1" ><span class="white_text">Delete Existing Emails and Upload New File for<br />
<strong><br />
<?php echo $campaign_selected; ?></strong></span><br />
<br />
<form id="form1" name="form1" method="post" action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<input type="hidden" name="campaign_id" id="campaign_id" value='<?php echo $campaign_id; ?>' />
<input type="submit" name="submit" id="submit" onclick="return confirm('Are you sure you want to delete the existing list?');" value="Continue >>" />
<br />
<td align="center" valign="top" bgcolor="#336666" class="rounded-corners1" >
<form id="form1" name="form1" method="post" action="mm_send_main_3.php">
<span class="white_text">Re-Send Message to<br />
the above list for<br />

<strong><br /><?php echo $latest_name; ?></strong></span><br />
<br />
<input type="hidden" name="campaign_id" id="campaign_id" value='<?php echo $latest_campaign; ?>' />
<input type="submit" name="button2" id="button2" onclick="return confirm('Are you sure you want to process the existing list?');" value="Continue >>" />
<br />

Answer Source

You have:

  $email_data = mysql_query("SELECT * FROM `massmail`");
  $row2 =mysql_fetch_array($email_data);

  ... lots of code ...

  while($row2 = mysql_fetch_array($email_data)){

That first fetch call right after you run the query fetches the first row of the result. As soon as you reach the while() call, that first $row2 result is destroyed and lost.

As eleazan points out below, you can reverse the loop into a do { ... } while() instead.

$row2 = mysql_fetch_array($email_data);
... do stuff
do {
   start output here
while ($row2 = msyql_fetch_array($email_data));

This will "preserve" that first row of data for your first iteration around the loop.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download