Forum Moderators: coopster

Message Too Old, No Replies

Mysql table keep crashing

keep crashing "online" table

         

jcodemasters

12:12 am on Sep 28, 2007 (gmt 0)

10+ Year Member



HI,
I have a website where I track online users.. I call a code that on every page that does this job.. and here is the code

<?php
function fill_online() {
global $USER, GET_CONFIG;
$query = "SELECT * FROM online WHERE `session_id` = '" . session_id() . "'";
$result = mysql_query( $query );

$ip = $_SERVER['REMOTE_ADDR'];
$page = $_SERVER['REQUEST_URI'];
$browser = $_SERVER['HTTP_USER_AGENT'];

if( $result && mysql_num_rows( $result ) )
{
$user_field = '';
if($USER!= '') {
$user_field = "`user`='$USER',";
}
else {
$user_field = "`user`='',";
}
$query = "UPDATE `online`
SET
$user_field
`browser` = '$browser',
`ip` = ' $ip',
`time` = '".date("YmdHi")."'
WHERE `session_id` = '" . session_id() . "'";
mysql_query($query);
}
else {
if ($USER!= '') {
$query = "INSERT INTO `online` (`session_id`, `user`, `time`, `ip`, `browser`, `page`)
VALUES('".session_id()."','$USER','". date( "YmdHi" )."','$ip','$browser','$page')";
}
else {
$query = "INSERT INTO `online` (`session_id`, `time`, `ip`, `browser`, `page`)
VALUES('".session_id()."', '". date( "YmdHi" )."','$ip','$browser','$page')";
}
mysql_query($query);
}
@mysql_free_result($result);

//DELETE
$now = date("YmdHi"); // yyyymmddhhmm, ex. 200009021431
$timer_ago = date("YmdHi", mktime(date("H"), date("i") - $GET_CONFIG['max_time_online'], 0,
date("m"), date("d"), date("Y")));
$query = "DELETE FROM `online` WHERE `time` < '" . $timer_ago ."'";
mysql_query($query);

}
?>

can anyone suggest me how to tweak this code?

jatar_k

12:29 am on Sep 28, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



is it a performance issue? is your script timing out? is it leaving connections open? is the above code working properly?

are you sure this code is causing the table to crash?

jcodemasters

1:18 am on Sep 28, 2007 (gmt 0)

10+ Year Member



Too many questions.. error generated :)

Yes above code works very fine with about 300+ users online.. at a time.. but whenever online users increase up to 400.. I get mysql error table "Table online is crashed" so all I have to login into cpanel and repair online table.

There should be a performance issue. because it keeps check online table.. Just wondering if there is any thing PHP where I can store global variable in server memory..

like static or if you have knowledge of ASP then there is Application object which resides in server memory.. If I found something like this then table crash wont happen

As far as I know this is due to too many table queries/deletion from online table

jcodemasters

1:19 am on Sep 28, 2007 (gmt 0)

10+ Year Member



between this.. I joined webmaster world on joined:Apr 30, 2006 and I am still new user.. this looks to me strange..

jatar_k

1:45 am on Sep 28, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I bet it has more to do with connections, you could be maxing them out

this would cause corruption, I've had the same problem a couple times, up the number of max_connections and things are ok.

You could maybe tone down the number of times it hits the db, maybe store a timestamp in the session and only have it write every x number of minutes, make x = to whatever lightens the load down to manageable levels

>> still new user

hehe, you're just the quiet type ;)

jcodemasters

2:48 pm on Sep 28, 2007 (gmt 0)

10+ Year Member



Yes i have already this thing in mind.. i was wondering if I could find something like global variable which reside all time in server memory