0

In the following section of code. I'm checking my $email_count 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";


                        }else{
            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; \" >";

// THIS IS WHERE I OUTPUT TO SCREEN BUT THE FIRST RECORD DOESN'T OUTPUT.    

            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">
          <tr>
            <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 />
        </form>
            </td>
            <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 />
        </form>
            </td>
          </tr>
      </table>
<?php
};
?>
2
  • Are you sure there's at least one record? Commented Jul 17, 2013 at 15:34
  • FYI: mysql is deprecated, consider switching to mysqli or pdo net.tutsplus.com/tutorials/php/… Commented Jul 17, 2013 at 15:35

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Adding to the answer... you can use do...while() for fix your problem fetching ;) php.net/manual/en/control-structures.do.while.php
Phenomenal!!! Thanks Mark B. Worked like a charm. I check your answer as accepted, I hope this question helps other people as well.
Brilliant. Thanks! I must remember to use do --- while more!
1

Try adding this before the while loop

mysql_data_seek($row2,0);

Each time you call a function like mysql_fetch_array, you are moving the internal row pointer within the MySQL result. As you've already called mysql_fetch_array, it has shifted the pointer thus not returning the first row within your loop

Comments

1

Here's your issue:

 $latest_data = mysql_query("SELECT * FROM campaign WHERE `id`='$latest_campaign'");
 $latest_row = mysql_fetch_array($latest_data);

....  

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

You're calling mysql_fetch_array right after your query; this loads the first record, and moves the pointer on to the next one.

In your loop, you're calling it again, and since the pointer is currently pointing at the second record, it starts outputting from there.

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.