Weird Queries

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Weird Queries

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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).
Post Reply