Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



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

Posts: 59
Trades: 0
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?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
riscphree is offline
Reply With Quote
View Public Profile Visit riscphree's homepage!
 
 
Register now for full access!
Old 05-26-2007, 04:56 AM Re: mysql error when traffic
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
add the actual error message to your "die" statement so you can see what went wrong

mysql_error()
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-29-2007, 03:12 AM Re: mysql error when traffic
Novice Talker

Posts: 11
Name: Jossielyn Lucero
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
prtt75 is offline
Reply With Quote
View Public Profile
 
Old 05-29-2007, 10:01 AM Re: mysql error when traffic
Average Talker

Posts: 29
Name: Amit Soni
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Zitku is offline
Reply With Quote
View Public Profile Visit Zitku's homepage!
 
Old 05-30-2007, 07:03 PM Re: mysql error when traffic
Skilled Talker

Posts: 59
Trades: 0
Thanks guys, I'll rework something now using those suggestions.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
riscphree is offline
Reply With Quote
View Public Profile Visit riscphree's homepage!
 
Reply     « Reply to mysql error when traffic
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.38618 seconds with 12 queries