tags:

views:

50

answers:

2

I made a while loop in php:

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

$search=mysql_query("SELECT * from chat ORDER BY id DESC LIMIT 1");
$row=mysql_fetch_assoc($search);
$type=$row['type'];
$id=$row['id'];
while($type!='n'){
  usleep(10000);
  $search=mysql_query("SELECT * from chat ORDER BY id DESC LIMIT 1");
  $row=mysql_fetch_assoc($search);
  $type=$row['type'];
}

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

mysql_close($conn);
echo $row['message'] . "<br/>";

?>

It always runs to see if there are new entries in the database, but I dont know what to do to keep the server from overloading. I tried usleep but I don't think it's enough.

Also I am getting this error "Fatal error: Maximum execution time of 60 seconds exceeded" and I know I can change the number of repetitions in the .ini file but would that be wise? Will it have a really bad effect on the servers performance?

A: 
Gaby
it does leave the loop because the UPDATE gets through and does its job, im just asking if there is something i can do for the server.
Petre
The UPDATE doesn't "get through" until after the loop terminates - which, as @Gaby said, is never.
Chris
dude, IT DOES! the rows are updated what dont you get? it works!
Petre
@Petre, it might get through only the first time by skipping the entire loop if the $type == 'n' .. if not it will enter the loop and never leave.. if not please explain the algorithm and also make sure the code you posted here is the same as the one you run ... check for matching `{}`.. unless you are expecting another event to happen from another page that will make this code leave the loop..
Gaby
@Petre, update answer..
Gaby
well it checks the first time and gets the value of $type and if there are no new entries it will be "o| from old, so "while" $type not equal "n" it checks the server and gives $type the new value, and it does this until it is "n" and then it updates the table and echos the entry.
Petre
+1  A: 

Update

usleep(10000);
set_time_limit(11000);

First answer

What are you trying to achieve with this code? The timeout message comes from the script usleep(10000).

Your code cannot detect immediate user input, your PHP code will run between user interactions with the server.

If you want to create a chat utility:

  • Send your messages via AJAX each time an user hits Enter or Send
  • Store the message received, along with the timestamp of the message received
  • Select an update time, say 3 seconds
  • Update the chat via AJAX, each update time
  • Return the message that are update time old, or newer
Ast Derek
it allways runs. on success, when the message was brought back, i call it again so it starts over and the response is very quick. im just asking if there is something i can do for the server.
Petre