duanlie7962
2014-05-15 11:39 阅读 71
已采纳

PHP - 在循环中多次使用mysql_fetch_assoc()。

I'm working on a really simple email notification system for newly added comments. It works like this:

  1. loop through project users
  2. for each user, loop through all comments written in the last hour
  3. if there are any comments that are not written by the user, push them to an array
  4. if comment array is not empty, send an email to the user

Here's some of the code:

<?php
$users_query = mysql_query("SELECT * FROM users");

$comments_query = mysql_query("SELECT * FROM comments c, users u WHERE c.date > DATE_SUB(NOW(), INTERVAL 1 HOUR) and c.user_id = u.id");

while ($user = mysql_fetch_assoc($users_query)) {
   $commentsArr = array();  

   while ($comment = mysql_fetch_assoc($comments_query)) {
      if ($comment['email'] != $user['email']) {
         array_push($commentsArr, $comment['comment']);
      }
   }

   // send $commentsArr by email to $user['email']
}
?>

The problem is that the same mysql_fetch_assoc cannot be used multiple times on a single page. I tried reseting the pointer using mysql_data_seek($comments_query, 0); but that just threw the following error:

Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 0 is invalid for MySQL result index 4 (or the query data is unbuffered)

Are there any other ways of handling this? All other people with similar questions were advised to join their tables instead of splitting them up. I don't think I can do that since I have to go through all users first, so if I join them, I'll have more rows than users.

Anyway, is there a way to loop through the comments while inside a loop?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    doubengshao8872 doubengshao8872 2014-05-15 13:12

    I switched from mysql to mysqli and now mysqli_data_seek($comments_query, 0); works as expected.

    点赞 评论 复制链接分享
  • dongquanlin1885 dongquanlin1885 2014-05-15 12:00

    Use $commentsArr array in outside of the while loop:

     <?php
        $users_query = mysql_query("SELECT * FROM users");
    
        $comments_query = mysql_query("SELECT * FROM comments c, users u WHERE c.date > DATE_SUB(NOW(), INTERVAL 1 HOUR) and c.user_id = u.id");
        $commentsArr = array();  
        while ($user = mysql_fetch_assoc($users_query)) {
    
    
           while ($comment = mysql_fetch_assoc($comments_query)) {
              if ($comment['email'] != $user['email']) {
                 array_push($commentsArr, $comment['comment']);
              }
           }
    
           // send $commentsArr by email to $user['email']
        }
        ?>
    
    点赞 评论 复制链接分享
  • douju1997 douju1997 2014-05-15 13:50
    <?php
    $users_query = mysql_query("SELECT * FROM users");
    $users = mysql_fetch_array($users_query);
    
    $comments_query = mysql_query("SELECT * FROM comments c, users u WHERE c.date >    DATE_SUB(NOW(), INTERVAL 1 HOUR) and c.user_id = u.id");
    
    $comments = mysql_fetch_array($comments_query)
    
    foreach($user as $users) {
      $commentsArr = array();  
    
      foreach($comment as $comments) {
        if ($comment['email'] != $user['email']) {
         array_push($commentsArr, $comment['comment']);
        }
      }
    
      // send $commentsArr by email to $user['email']
     }
    ?>
    
    点赞 评论 复制链接分享

相关推荐