Page 1 of 1

Weird Queries

Posted: Thu May 20, 2004 4:45 pm
by John Cartwright

Code: Select all

<?php
<?                                                                               
include "mysql.php";

$t_stamp = time();                                                                                            
$timeout = $t_stamp - $to_secs; 
                                                                 
@mysql_query("INSERT INTO usersonline SET timestamp='$t_stamp', ip='$REMOTE_ADDR',file='$file'")   or die("Database INSERT Error");
 
@mysql_query("DELETE FROM usersonline WHERE timestamp<$timeout")                                  or die("Database DELETE Error");    
 
$result = mysql_query("SELECT DISTINCT `ip` FROM `usersonline`")                                  or die("Database SELECT Error");

$user = mysql_num_rows($result);                                                                                                                                                             

echo "Users Online: <b>$user</b><br>";


?>
?>
Currently I am running a server from this comp so it's my localhost, therefor everything is really fast loading. When I click on the links slowly, like 2 secs apart everything works fine. But LEts say I click any link two times quickly, It goes to the DIE statement. The mysql db is also on the same server ( localhost ). Any explaination for this? Is it simply just not getting the data fast enough? Or does it have to process the data from the first clicked link before starting the 2nd?

Posted: Thu May 20, 2004 4:50 pm
by feyd
I believe that your second click happens in the middle of a query, where the server receives a cancel on the first click, which probably makes the query fail and die.

Posted: Thu May 20, 2004 4:55 pm
by tim
feyd wrote:I believe that your second click happens in the middle of a query, where the server receives a cancel on the first click, which probably makes the query fail and die.
exactly

Posted: Thu May 20, 2004 5:05 pm
by John Cartwright
Hrmm... I tried doing something like this

Code: Select all

<?php
include "mysql.php"; 

$t_stamp = time();                                                                                            
$timeout = $t_stamp - $to_secs; 
                                                                  
@mysql_query("INSERT INTO usersonline SET timestamp='$t_stamp', ip='$REMOTE_ADDR',file='$file'")   or die(

javascript redirect: index.php?error

);

@mysql_query("DELETE FROM usersonline WHERE timestamp<$timeout")                                  or die("Database DELETE Error");    

$result = mysql_query("SELECT DISTINCT `ip` FROM `usersonline`")                                  or die("Database SELECT Error"); 

$user = mysql_num_rows($result);                                                                                                                                                              

echo "Users Online: <b>$user</b><br>"; 


?> 
?>
Then I have on my main page a function that detects what page to load in the middle via GET. But the problem is that the page will keep on being relaoded... for obvious reasons.. duh. Anyone got any ideas?

Posted: Thu May 20, 2004 7:18 pm
by launchcode
feyd wrote:I believe that your second click happens in the middle of a query, where the server receives a cancel on the first click, which probably makes the query fail and die.
I would be doubtful of this - MySQL is set to handle all row-locking functions automatically. If you run an INSERT statement it'll lock the table until it has completed before allowing any other operation on it (regardless of who requests that). This row locking happens so fast you don't notice it, but it does happen.

The OP didn't say *which* statement died. It's far more likely that the record is being deleted before it's able to have retrieved it again (select distinct).