...

View Full Version : users online



Blatch
08-30-2009, 09:20 PM
<?php
session_start();
$session = session_id();
$time = time();
$time_check = $time - 600; // 10 minutes

$username = "";
$password = "";
$tbl_name = "";

mysql_connect("localhost", $username, $password);
mysql_select_db($tbl_name);
$count = mysql_num_rows(mysql_query("SELECT * FROM `user_online` WHERE `session` = '$session'"));

if ($count=="0") {
mysql_query("INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')");
} else {
mysql_query("UPDATE $tbl_name SET time='$time' WHERE session = '$session'");
}

$usersonline = mysql_num_rows(mysql_query("SELECT * FROM $tbl_name"));

mysql_query("DELETE FROM $tbl_name WHERE time < $time_check"); // Delete session over time limit
mysql_close();
?>

I keep getting mysql_num_rows(): supplied argument is not a valid MySQL.. Why?

the-dream
08-30-2009, 09:22 PM
On which line?

the-dream
08-30-2009, 09:26 PM
Ok, I think I see your problem...


<?php
session_start();
$session = session_id();
$time = time();
$time_check = $time - 600; // 10 minutes

$username = "";
$password = "";
$tbl_name = "";

mysql_connect("localhost", $username, $password);
mysql_select_db($tbl_name);
$count = mysql_num_rows(mysql_query("SELECT * FROM user_online WHERE user_online.session = '$session'"));

if ($count=="0") {
mysql_query("INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')");
} else {
mysql_query("UPDATE $tbl_name SET time='$time' WHERE session = '$session'");
}

$usersonline = mysql_num_rows(mysql_query("SELECT * FROM $tbl_name"));

mysql_query("DELETE FROM $tbl_name WHERE time < $time_check"); // Delete session over time limit
mysql_close();
?>

Try that...

_Aerospace_Eng_
08-30-2009, 09:48 PM
I see an issue with this. You have this

$tbl_name = "";
but then you use it here

mysql_select_db($tbl_name);
Your $tbl_name is NOT your database name. You then use this here

INSERT INTO $tbl_name(session, time)VALUES('$session', '$time')
So now $tbl_name has become an actual table and not a database. You can't insert directly into a database like that. You need to have database that has tables (this is where you insert, update, and delete from). I suspect your queries are erroring out but you aren't checking for any errors.

<?php
session_start();
$session = session_id();
$time = time();
$time_check = $time - 600; // 10 minutes

$username = "";
$password = "";
$tbl_name = "";
$db_name = "";

mysql_connect("localhost", $username, $password);
mysql_select_db($db_name);
$result = mysql_query("SELECT * FROM $tbl_name WHERE `session` = '$session'") or die(mysql_error());
$count = mysql_num_rows($result);

if ($count==0) {
mysql_query("INSERT INTO $tbl_name(session, time) VALUES ('$session', '$time')") or die(mysql_error());
} else {
mysql_query("UPDATE $tbl_name SET time='$time' WHERE session = '$session'") or die(mysql_error());
}
$result2 = mysql_query("SELECT * FROM $tbl_name") or die(mysql_error());
$usersonline = mysql_num_rows($result2);

mysql_query("DELETE FROM $tbl_name WHERE time < $time_check") or die(mysql_error()); // Delete session over time limit
mysql_close();
?>
Update $db_name, and try that. Let us know if you get any errors.

the-dream
08-30-2009, 09:50 PM
Ah, I didn't see that. Silly me.

Blatch
08-30-2009, 10:05 PM
Ahhh! Thank you :)

How poorly coded was this?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum