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)

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

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址