[SOLVED] HELP - Users Online PHP Script not working.

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
camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

[SOLVED] HELP - Users Online PHP Script not working.

Post by camarosource »

I tried this users online script I found and it's not working. When you run it, it says

"Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/camaro92/public_html/useronline.php on line 20
No Database Selected"

http://www.camarosource.ca/useronline.php

Help! Thanks.

Below is the code. Obviously I hid the user/pass/db names

Code: Select all

<? 
$server = "localhost";    // Your mySQL Server, most cases "localhost"                  
$db_user = "[HIDDEN]";   // Your mySQL Username                                        
$db_pass = "[HIDDEN]";  // Your mySQL Password                                        
$db = "[HIDDEN]"; // Database Name  

mysql_connect($server, $db_user, $db_pass) or die ("Useronline Database CONNECT Error");                                                                   

//REMEMBER TO CONNECT TO THE DATABASE!! 


$ip =            $_SERVER['REMOTE_ADDR']; 
$time =             time(); 

$ip_check = 'SELECT ip FROM `users_online` WHERE ip = '''.$_SERVER['REMOTE_ADDR'].''''; 
$ip_query = mysql_query($ip_check); 
$num_rows = mysql_num_rows($ip_query); 

if(!$num_rows) { //if no rows, meaning no ip's in db matched theirs, we will add them. 
   $insert_new = mysql_query("INSERT INTO `users_online` (ip, time) VALUES ('$ip', '$time')");   
} //end if NOT THERE 

//this means that they're already in there, so we update info. 
   if($num_rows > 0) { 
      $update = mysql_query('UPDATE `users_online` SET time='''.time().''' WHERE ip = '''.$_SERVER['REMOTE_ADDR'].''''); 
       if(!$update) die(mysql_error()); 
} // end UPDATE 

//if time now - start time > 300 then its been 5 minutes, so we delete 
   $delete_old = mysql_query("DELETE FROM `users_online` WHERE ((".time()."-time) > 300)"); 
     if(!$delete_old) die(mysql_error()); 

//show the number of people online now.... 
$id_query = @mysql_query("SELECT * FROM `users_online`"); 
$users_online = mysql_num_rows($id_query); 

echo("<BR>The number of people here are : $users_online<BR>"); 
//users online is done.... 
?>
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

w.geoghegan
Forum Newbie
Posts: 7
Joined: Mon Jan 03, 2005 7:27 pm
Location: Bucks, UK.

Post by w.geoghegan »

Place this line after the mysql_connect line:

mysql_select_db($db) or die("Unable to select database.<br>Check database exists.");

Cheers.
camarosource
Forum Commoner
Posts: 77
Joined: Sat Aug 03, 2002 10:43 pm

Post by camarosource »

works now. Thanks all.
Post Reply