Reply
mysql error when traffic
Old 05-25-2007, 08:09 PM mysql error when traffic
Skilled Talker

Posts: 59
I'm using this script that displays the number of users currently online using mysql and php. However, when two users visit the site at the exact same time one user will be able to see it and the other will get the error:

Useronline Database INSERT Error

The database schema is:
Code:
CREATE TABLE useronline (
   timestamp int(15) DEFAULT '0' NOT NULL,
   ip varchar(40) NOT NULL,
   file varchar(100) NOT NULL,
   PRIMARY KEY (timestamp),
   KEY ip (ip),
   KEY file (file)
);
The php script is:
Code:
$timeoutseconds = 300; // Timeout value in seconds                                                                                                       

$timestamp=time();                                                                                            
$timeout=$timestamp-$timeoutseconds;  
mysql_connect($server, $db_user, $db_pass) or die ("Useronline Database CONNECT Error");                                                                   
mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')") or die("Useronline Database INSERT Error"); 
mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout") or die("Useronline Database DELETE Error");
$result=mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'") or die("Useronline Database SELECT Error");
$user  =mysql_num_rows($result);                                                                              
mysql_close();                                                                                                
if ($user==1) {echo"<br /><b>$user User online</b>";} else {echo"<br /><b>$user Users online</b>";}
?>
Anyway I might get around this? Do I just need to code something new?
riscphree is offline
Reply With Quote
View Public Profile Visit riscphree's homepage!
 
When You Register, These Ads Go Away!
Old 05-26-2007, 03:56 AM Re: mysql error when traffic
chrishirst's Avatar
Super Moderator

Posts: 13,587
Location: Blackpool. UK
add the actual error message to your "die" statement so you can see what went wrong

mysql_error()
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-29-2007, 02:12 AM Re: mysql error when traffic
Novice Talker

Posts: 10
Name: Jossielyn Lucero
If two users visit the site at the exactly the same time and you set your (timestamp) as primary key, it will not accept a duplicate record based on your primary key and therefore will result to an error inserting the second record.
prtt75 is offline
Reply With Quote
View Public Profile
 
Old 05-29-2007, 09:01 AM Re: mysql error when traffic
Average Talker

Posts: 29
Name: Amit Soni
Please don't use timestamp as the primary key.
Keep the primary key as autoincrement and if you need timestamp in the table, add it to a separate field.

--Amit.
Zitku is offline
Reply With Quote
View Public Profile Visit Zitku's homepage!
 
Old 05-30-2007, 06:03 PM Re: mysql error when traffic
Skilled Talker

Posts: 59
Thanks guys, I'll rework something now using those suggestions.
riscphree is offline
Reply With Quote
View Public Profile Visit riscphree's homepage!
 
Reply     « Reply to mysql error when traffic
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14073 seconds with 12 queries