0

在下面的代码部分。我正在检查我的$email_count并输出两件事之一。这工作正常。问题出在while循环中。它不会回显第一条记录。

// 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
};
?>
4

3 回答 3

2

你有:

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

  ... lots of code ...

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

运行查询后的第一次fetch调用会获取结果的第一行。一旦你while()接到电话,第一个$row2结果就会被破坏并丢失。

正如 eleazan 在下面指出的那样,您可以将循环反转为 a do { ... } while()

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

这将为您在循环中的第一次迭代“保留”第一行数据。

于 2013-07-17T15:36:34.857 回答
1

尝试在 while 循环之前添加它

mysql_data_seek($row2,0);

每次调用类似的函数时mysql_fetch_array,都会在 MySQL 结果中移动内部行指针。正如您已经调用的那样mysql_fetch_array,它已经移动了指针,因此没有返回循环中的第一行

于 2013-07-17T15:34:48.407 回答
1

这是你的问题:

 $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)){

您在查询后立即调用 mysql_fetch_array ;这将加载第一条记录,并将指针移至下一条记录。

在您的循环中,您再次调用它,并且由于指针当前指向第二条记录,因此它从那里开始输出。

于 2013-07-17T15:37:51.317 回答