Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1006255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:25:44+00:00 2026-05-16T08:25:44+00:00

i made a chat using php and ajax, and i am using a while

  • 0

i made a chat using php and ajax, and i am using a while loop to check the database for new messages.

this is the code that retrieves the message:

//retrive message

function update(){

$(document).ready(function(){

$.ajax({

async: true,

type: "POST",

url: "listen.php",

success: function(data){

$("#myp").before(data);

},

complete: function(){

window.setTimeout("update();",100);
}

});

});

};


//wating for new message

<?php
include_once("connect.php");

$type="";

while($type!=='n'){


usleep(1000);

$search=mysql_query("SELECT * from chat ORDER BY id DESC LIMIT 1");

$row=mysql_fetch_assoc($search);

$type=$row['type'];

$id=$row['id'];

}

echo $row['message'] . "<br/>";


mysql_query("UPDATE chat SET type='o' WHERE id=$id");


?>

now this works fine,the php file constantly checks to see if there are any new messages, the update function starts when the page is loaded and waits for the response.
but is it efficient? if i would use this on a website, im afraid it would stress the server too much, because of the while loop. does anyone know of a way, to make that while loop more server friendly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:25:45+00:00Added an answer on May 16, 2026 at 8:25 am

    Your hunch is correct. A typical server setup can answer any where between 100-1000 PHP requests per second, so doing 10 requests a second per client is going to eat up alot of resources on your server. While it might work create for a few people, it’s not going to scale well. Your server might max out anywhere between 10-100 users (which is rather low).

    One fix is to increase the time between each server poll, but this is only a linear fix and will degrade the experience of the users.

    A better solution might be to use a comet approach. This is hard (if not impossible) to do purely with PHP, so you will need to deal with some external API to handle the long http requests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a while loop in php: <?php include_once(connect.php); $search=mysql_query(SELECT * from chat ORDER
I've made a fairly basic AJAX chat with PHP & MySQL. The chat messages
I have made an AJAX chat using Javascript, PHP, and MySQL. I send the
I have made a simple chat application in PHP/Ajax/MySQL. I am regularly calling these
I have made a very successful PHP chat box that saves the chat to
I made a chat using PHP and JavaScript chat and there is a disconnect
I have made a simple chat script using jquery and AJAX but the huge
I made a simple chat application that connects the server and client using the
For my custom made chat screen i am using the code below for checking
I have made a chat script using php, mysql and jquery. It uses json

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.