dttphb59319 2014-07-21 09:55
浏览 36
已采纳

PHP执行持久数据库检查

I'm actually trying to create a ( more or less ) instant chat. There's just one thing left to go, until it's finished. And that's a DB check for new entries every second by PHP itself. A while loop works great for it, because i can exit it perfectly. The Problem is, that the "every second" thing isn't working. I tried a sleep(1), but that causes a 1 minute server freeze, until the script has been completed. Hope someone can help me, i'm actually frustrated about this problem.

    elseif($latestID != 'undefined' && $_POST['returnafter'] == '60')
    {
      $timeout = '60';
      $i = '0';
      while ($i != $timeout)
      {
      $chat_content = "";
      $i++;
        $getLastID = mysql_query("SELECT id, userid, content, time_posted FROM (SELECT * FROM chat_system ORDER BY id DESC LIMIT 150) chat_system WHERE id > '" . $latestID . "' $extraQuery ORDER BY id ASC");
        while ($lastID = mysql_fetch_object($getLastID))
        {
          $rowID = $lastID->id;
          $user_posted_id = $lastID->userid;
          $chat_content = $lastID->content;
          $chat_posted = $lastID->time_posted;
          $getUserData = mysql_query("SELECT username, avatar, loggedIn FROM account_data WHERE account_id=('" . $user_posted_id . "')");
          while ($userData = mysql_fetch_object($getUserData))
          {
            $username = $userData->username;
            $useravatar = $userData->avatar;
            $loginStatus = $userData->loggedIn;
          }

          if ($loginStatus == '1')
          {
            $onlineStatus = '<img src="./images/3DArt/newOnline.png" class="chat_onlineStatus">';
          }
          else
          {
            $onlineStatus = '<img src="./images/3DArt/newOffline.png" class="chat_onlineStatus">';
          }

          if (date('Y-m-d', $chat_posted) == date('Y-m-d'))
          {
            $time_posted = strftime('Heute, %H:%M', $chat_posted);
          }
          elseif (date('Y-m-d', $chat_posted) == date('Y-m-d', strtotime("Yesterday")))
          {
            $time_posted = strftime('Gestern, %H:%M', $chat_posted);
          }
          elseif (date('Y-m-d', $chat_posted) < date('Y-m-d', strtotime("Yesterday")))
          {
            $time_posted = strftime("%A, %d %B %Y %H:%M", $chat_posted);
          }

          if (isset($_POST['parseEmoticons']) && $_POST['parseEmoticons'] == 'true')
          {
            $chat_content = emoticons($chat_content);
          }

          $newChatRow.= '<div class="chatRow" id="' . $rowID . '">
  <div class="chatRow_container">
  <div><img src="' . $useravatar . '" class="chatAvatar">' . $onlineStatus . '<a href="/?page=Profile&amp;User=' . $user_posted_id . '"><b>' . $username . '</b></a>
    </div>
  </div>' . $modActions . '
    <div class="chat_mainMsg">
        ' . $chat_content . '
    </div>
    <div class="chatTime">' . $time_posted . '</div>
</div>';
        }

        $content = str_replace(array(
          '
',
          '',
          '
'
        ) , "<br />", $newChatRow);


        if(!empty($chat_content)) { echo $newChatRow; $i = $timeout; return false; }
        if(empty($chat_content)) { sleep(1); return true; }

      }
    }

I think, this informations should be enough. If not, just ask.

EDIT: The request is initialized by Ajax and on success the request repeats. That's why PHP have to check for 60 seconds.

  • 写回答

1条回答 默认 最新

  • douhao2026 2014-07-21 10:05
    关注

    you should let the clientside decide what messages it "like" to receive. eg you call your messages.php class - per ajax on the client side and pass a timestamp when the last time was the script requested new messages

    example:

    ajax interval with 1 second loop call the messages.php?last=123456 and receive all messages that were created after or equal this timestamp. now you update in javascript the last timestamp and so on.

    or you use a perfect framework (if possible) that is designed perfectly for your task to solve this.

    http://socket.io/ (or js keyword websockets)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效