View Full Version : PHP Question
Trigs
31-12-2009, 01:02 AM
Haven't done anything in PHP in around 6 months so I don't remember.
How do you count the number of users in a table again?
Also, how do I see what users are online? My system uses sessions so I guess I would set a cookie?
Sorry for the simple questions, but again, I haven't done anything in around 6 months.
Trigs
31-12-2009, 01:38 AM
Also, I'm getting:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/fazon/public_html/test/config.php on line 42
It's coming from:
// User's info
$query = mysql_query("SELECT * FROM users WHERE username = '{$_SESSION['username']}'");
$users = mysql_fetch_array($query);
RastaLulz
31-12-2009, 02:13 AM
To count the number of users you have you use mysql_num_rows()
Example:
<?php
include("config.php"); //where MySQL connection details are
$query = mysql_query("SELECT * FROM users"); //gets info from users
$count = mysql_num_rows($query); //counts the amount of users
echo $count; //echos the amount of users
?>I don't know anything about PHP and sessions.
Blinger1
31-12-2009, 02:27 AM
For the users online:
http://www.pixel2life.com/viewtutorial/68187/users_online_system_tutorial/
http://www.pixel2life.com/viewtutorial/59569/php_how_many_users_are_online/
either one
Trigs
31-12-2009, 03:12 AM
Thanks guys, what about the error I get?
RastaLulz
31-12-2009, 04:49 AM
Well, I don't know how to do it via sessions, but I could show you another way I do it. It may not be the best way of doing it, but it gets the job done.
First, I added a column to my "database", or whatever it's called to users called last_active. Every time a user is logged in, it gives them a time stamp and adds it to last_active (every page they load while logged in it updates the time stamp).
//last active
if($logged) {
$time = date("omdHis");
$updatetime = mysql_query("UPDATE users SET `last_active` = '$time' WHERE `username` = '$logged[username]'");
}else{
}Then I use the time stamp with a simple function that I created.
function whosonline() {
//current time
$timestamp = date("omdHis");
//subtracts 15 minutes of the current time
$subtract = (date('omdHis', strtotime('- 15 minutes')));
$result = mysql_query("SELECT * FROM users WHERE `last_active` > '$subtract' ORDER BY username DESC");
if(1 > mysql_num_rows($result)) {
echo "There's currently no user online.";
}else{
//loops until everyone has been added
while($online = mysql_fetch_array($result)) {
//the users information
$onlineuser = $online['username'];
echo '<a href="profile/'.$onlineuser.'">'.$onlineuser.'</a> ';
}
}
}As I said above, it may not be the best, but it gives a rough idea of how you could do it.
RastaLulz
31-12-2009, 05:59 PM
Ugh sorry for the double post (but we can't edit our previous posts which is lame), but I think that your getting an error because of the 's wrapped around username in the SQL query.
It should look like the following:
$_SESSION[username]
LMS16
01-01-2010, 02:05 PM
Ugh sorry for the double post (but we can't edit our previous posts which is lame), but I think that your getting an error because of the 's wrapped around username in the SQL query.
It should look like the following:
$_SESSION[username]
Thats surely doesn't matter? I use $_SESSION["name"]; & $_SESSION['name']; all the time without errors...
Lew.
RastaLulz
01-01-2010, 05:11 PM
Thats surely doesn't matter? I use $_SESSION["name"]; & $_SESSION['name']; all the time without errors...
Lew.
It surely gives me errors when I do it because it's cutting off the string (or whatever) when you do that.
RastaLulz
01-01-2010, 05:33 PM
Sigh, something really needs to be done about editing posts.. :eusa_pray
But now that I think of it, it most likely isn't that because it'd return:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\xampp\htdocs\yoursite\file.php on line lineSo, it's most likely that $_SESSION['username']; doesn't mean anything, because there's no argument for MySQL, but then again I could be wrong. But mysql_fetch_array has nothing to get, therefore it's putting out an error.
Trigs
03-01-2010, 02:39 AM
Sorry, it was because I forgot to create the tables. lmao.
iDenning
03-01-2010, 06:36 PM
Sorry, it was because I forgot to create the tables. lmao.
epic failure haha.
Thats like asking why your toast isnt right, then realising you dont have a toaster lol.
Glad you fixed it though :)
Trigs
03-01-2010, 07:23 PM
Haven't done anything in PHP in about 6 months, so I started learning again.
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.