Jump to content

Recommended Posts

i am writing a website that has a chat board for 1:1 online tutoring.  i did not write the chat app, and do not have access to it's database.  what i need to figure out is how to redirect the user once the tutor logs off.  currently, when a tutor logs in, it sets a value in a mysql database seperate to the chat one.  i want to find a way to query the db every 2 minutes, and if the admin is not logged on, redirect the user.  how can i use sleep in an while loop?

Link to comment
https://forums.phpfreaks.com/topic/170428-timed-loop/
Share on other sites

This code is rough but should work..

$result = @mysql_query("SELECT * FROM whatever");
while ($record=mysql_fetch_array($result)) {
echo $record["adminonline"];
flush(); //Prevent output buffer from sleeping
sleep(60*2); //Two minutes
}

You'd need to rewrite the while loop a bit to suit it to your own needs to finding if the admin/tutor is online, but sleep() will stop it from running the script continuously, flush() will prevent the script from sleeping, and make it just the while loop.

Link to comment
https://forums.phpfreaks.com/topic/170428-timed-loop/#findComment-899006
Share on other sites

$result = @mysql_query("SELECT * FROM whatever");
while ($record=mysql_fetch_array($result)) {
echo $record["adminonline"];
sleep(60*2); //Two minutes
flush(); //Prevent output buffer from sleeping
}

 

First of all, why  use the @ (error-suppressing operator)?

$result = @mysql_query("SELECT * FROM whatever");

 

mysql_query() only returns a resource or true or false, so no need to suppress.

 

The code also need a slight modification or will throw a nasty error otherwise when your query fails and you try to access it with mysql_fetch_array() as it expects a resource and gets a boolean (false).

 

Also don't use mysql_fetch_array() if you only use the the associative part, use mysql_fetch_assoc() instead.

 

mysql_fetch_array() returns the result both as a numerical and as a associative array (unless a second parameter MYSQL_NUM or MYSQL_ASSOC is provided by default MYSQL_BOTH) thus doubling the result (numerical and associative instead of only the associative part) + your memory is affected to, the cached mysql result set + your array that contains the result in both a numerical and an associative array.

 

$result = mysql_query("SELECT * FROM whatever");
if ($result && mysql_num_rows($result)) {
    while ($record=mysql_fetch_assoc($result)) {
        echo $record["adminonline"];
        sleep(60*2); //Two minutes
        flush(); //Prevent output buffer from sleeping
    }
}//Else: Query failed

Link to comment
https://forums.phpfreaks.com/topic/170428-timed-loop/#findComment-899014
Share on other sites

<?php

$link = mysql_connect('localhost', '*****', '*****');
if (!$link) {
    die('Error connecting to the DB.');
}

$db_selected = mysql_select_db('*****', $link);
if (!$db_selected) {
    die ('Error connecting to the DB.');
}
$result = mysql_query("SELECT * FROM *****");


if ($result && mysql_num_rows($result)) {
    while ($record=mysql_fetch_array($result)) {
        echo $record["is_loggedin"];
        sleep(5); //5 secs for testing purposes
        flush(); //Prevent output buffer from sleeping
    }
}//Else: Query failed
mysql_close($link);
?>

 

is_loggedin is a boolean in mysql.  the page waits 5 secs for localhost, then shows whatever is in the db.  i go back to the mysql query browser, and the page does not change.

Link to comment
https://forums.phpfreaks.com/topic/170428-timed-loop/#findComment-899020
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.