Current location: Hot Scripts Forums » Programming Languages » PHP » is this correct?


is this correct?

Reply
  #1 (permalink)  
Old 12-30-03, 05:20 PM
darkcarnival's Avatar
darkcarnival darkcarnival is offline
PHP/MySQL coder
 
Join Date: Jun 2003
Posts: 939
Thanks: 0
Thanked 0 Times in 0 Posts
is this correct?

hi,

i made a whos online snipplet and im not sure if i coded it right, since i added a few things that wasnt found on the tutorial i used. heres my code plz tell if its correct or not.PS i would test it myself but i want to get this done quickly so i can have more tike t to finish the rest of the program.

PHP Code:

$time =time();

$timeout "$time-600";

include 
"config.php";
$db_connection mysql_connect ($DB_HOST$DB_USER$DB_PASS) or die ('Could not connect to MySQL: ' mysql_error());

$query mysql_query("SELECT * FROM Metal_Online WHERE Username='$username'");
$result mysql_db_query ($DB_NAME$query);

$del mysql_query("DELETE FROM Metal_Online WHERE time<$timeout");
$result_del mysql_db_query ($DB_NAME$del);

$num_user mysql_num_rows($result);
if ((
$result) && ($row=mysql_fetch_array($result)))
            {
$online $row["Username"];
}
else
            {
              echo 
mysql_errno().": ".mysql_error()."<BR>";
            }

            
mysql_close ();
 
?> 
the rest was just variables to display the process. so is this coded right? if not plz tell me what i missed or what i should add.thanks

Last edited by darkcarnival; 12-30-03 at 05:34 PM.
Reply With Quote
  #2 (permalink)  
Old 12-30-03, 10:31 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

I think your code will probably give you an error.

Why are you using mysql_db_query() on the result? If you've queried out the result already with mysql_query(), you don't need to do it again - besides, mysql_db_query() takes an SQL statement as the second parameter - but you set the result, which is not correct.

Here's also something I quote from php.net regarding mysql_db_query():

Quote:
Originally Posted by http://ca.php.net/function.mysql-db-query
Note: This function has been deprecated since PHP 4.0.6. Do not use this function. Use mysql_select_db() and mysql_query() instead.
Hope this helps. =)
__________________
Blavv =|
Reply With Quote
  #3 (permalink)  
Old 12-31-03, 08:44 AM
darkcarnival's Avatar
darkcarnival darkcarnival is offline
PHP/MySQL coder
 
Join Date: Jun 2003
Posts: 939
Thanks: 0
Thanked 0 Times in 0 Posts
hi,

ok how should it be done? i use that kind of query in all of my programs since i found that on a tutorial. but i tried that 1 u say to try and it always says need db name so i just do it like that.
Reply With Quote
  #4 (permalink)  
Old 12-31-03, 04:10 PM
darkcarnival's Avatar
darkcarnival darkcarnival is offline
PHP/MySQL coder
 
Join Date: Jun 2003
Posts: 939
Thanks: 0
Thanked 0 Times in 0 Posts
ok i fixed the query thing so what should i do so it wont display a error?
Reply With Quote
  #5 (permalink)  
Old 12-31-03, 05:07 PM
YourPHPPro's Avatar
YourPHPPro YourPHPPro is offline
Community VIP
 
Join Date: Aug 2003
Posts: 430
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by darkcarnival
what should i do so it wont display a error?
Havent tested this out, but this should get you started in the right direction:

Code:
<script language="php">
// No need to set time variable if we just use it once - include it in the SQL
// $time =time(); 
// $timeout = "$time-600"; 

include "config.php"; 
$db_connection = mysql_connect ($DB_HOST, $DB_USER, $DB_PASS) or die ('Could not connect to MySQL: ' . mysql_error()); 

// Since it doesnt appear that we actually need the usernames, lets just count how many there are.
$query = "SELECT COUNT(Distinct Username) AS NowOnline FROM Metal_Online WHERE time>(NOW()-600)";
$result = mysql_db_query ("$DB_NAME", "$query", $db_connection);
// Only one result, so we only need to do this one time
$Online = mysql_fetch_array($result, MYSQL_ASSOC);
//$Online["NowOnline"] should contain the total number of visitors online
// Now lets do cleanup - 5% chance of it happening.
if (rand (1,100)<=5) {
	$dump = mysql_query("DELETE FROM Metal_Online WHERE time<(NOW()-600)");
}
</script>
Reply With Quote
  #6 (permalink)  
Old 01-01-04, 09:26 AM
darkcarnival's Avatar
darkcarnival darkcarnival is offline
PHP/MySQL coder
 
Join Date: Jun 2003
Posts: 939
Thanks: 0
Thanked 0 Times in 0 Posts
ok but i think username would be needed so it can count the usersnames. also will this display the users?
Reply With Quote
  #7 (permalink)  
Old 01-02-04, 09:48 AM
darkcarnival's Avatar
darkcarnival darkcarnival is offline
PHP/MySQL coder
 
Join Date: Jun 2003
Posts: 939
Thanks: 0
Thanked 0 Times in 0 Posts
ok i fixed it up from looking at the examples giving now i have to have it display how many visitors are on my site. so how do i do that?thx.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
not displaying the correct info sparky PHP 8 06-26-03 08:28 AM


All times are GMT -5. The time now is 07:49 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.