PDA

View Full Version : What am I doing wrong here?


N9ne
02-13-2003, 07:28 PM
<?php
require('forum/admin/config.php');
$db = mysql_connect($servername,$dbusername,$dbpassword) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

unset($servername,$dbusername,$dbpassword,$dbname) ;

$adminonline = mysql_query("SELECT userid FROM session");

if ($adminonline['userid'] == 1) {
echo("admin is online");
} else {
echo("admin is not online!");
}

?>


I'm trying to see if userid 1 is online on the forum, and if he is, it will say admin is online. However, it just shows admin is not online!

Why is it doing this? Is there something i've missed out here?

Xenon
02-13-2003, 07:35 PM
this line $adminonline = mysql_query("SELECT userid FROM session");

results in a query result, not an array.
you have to use a fetch array command (as vb does nearly always).

also you should use WHERE userid=1 in the query :)

N9ne
02-13-2003, 07:40 PM
Well if I select userid, I can do the if conditions for multiple userids ;)

I don't know how to do a fetch_array command, please could you show me how it would be done for me to achieve the correct results in my situation? Last time I needed help with hack (the intval function), you showed me how it was done, and then I managed to use it in another hack successfully! :)

Xenon
02-13-2003, 07:53 PM
hmm, i don't know the mysql php commandos very good.., i always use the $DB_site var from vb ;)

it would look something like that:

$adminonline = mysql_fetch_array(mysql_query("SELECT COUNT(userid) as total FROM session WHERE userid IN(1,4,5)"));

if ($adminonline['total'] > 1) {
echo("admin is online");
} else {
echo("admin is not online!");
}



in the IN () you can speciafy the userids of all your admins

N9ne
02-13-2003, 07:58 PM
I noticed that in index.php, in the code for online users, usergroupid is being called from the session table, I looked in the session table and there is no usergroupid field?! How can it be calling usergroupid if it isn't in the table?

Also, if I want to base it on usergroupid and not userid, how would I do that? I mean, it's definitely possible but the fact that there's no usergroupid field in the session table confuses me as I can't just call it because it doesn't exist!

Xenon
02-13-2003, 08:03 PM
in index.php there was used a joined query..

it's also possible but slows down the query a bit.

in that case it would look like that:
$adminonline = mysql_fetch_array(mysql_query("SELECT COUNT(userid) as total FROM session LEFT JOIN user USING(userid) WHERE user.usergroupid=6"));

if ($adminonline['total'] > 1) {
echo("admin is online($adminonline[total])");
} else {
echo("admin is not online!");
}

N9ne
02-13-2003, 08:17 PM
Ok so I'm almost there, I have one more question, what is the significance of 'total' in the query? What is it implying, what does it mean? This is a bit new to me as I'm used to just running a simple query, then using specific if conditions such as if ($adminonline['usergroupid'] == 6) but that's written into the query...

Xenon
02-13-2003, 08:18 PM
total is just a counter, it will save the ammount of admins online :)

N9ne
02-13-2003, 08:25 PM
So I can go one step further and do this:


$adminonline = mysql_fetch_array(mysql_query("SELECT COUNT(userid) as total FROM session LEFT JOIN user USING(userid) WHERE user.usergroupid=6"));

$adminonline['total'] = $admintotal;

if ($adminonline['total'] > 1) {
echo("There are $adminonline[total] admins online");
} elseif ($adminonline'total'] = 1) {
echo("There is $adminonline[total] admin online");
} else {
echo("There are no admins online");
}


...and this would be perfectly valid?

Xenon
02-13-2003, 09:05 PM
two mistakes in it:

$adminonline = mysql_fetch_array(mysql_query("SELECT COUNT(userid) as total FROM session LEFT JOIN user USING(userid) WHERE user.usergroupid=6"));


if ($adminonline['total'] > 1) {
echo("There are $adminonline[total] admins online");
} elseif ($adminonline['total'] == 1) {
echo("There is $adminonline[total] admin online");
} else {
echo("There are no admins online");
}

N9ne
02-13-2003, 09:08 PM
This line:


$adminonline = mysql_fetch_array(mysql_query("SELECT COUNT(userid) as total FROM session LEFT JOIN user USING(userid) WHERE user.usergroupid=6"));


is giving this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Xenon
02-13-2003, 09:25 PM
hmm, should work normally...
maybe just try this
$adminsql = mysql_query("SELECT COUNT(userid) as total FROM session LEFT JOIN user USING(userid) WHERE user.usergroupid=6");
$adminonline=mysql_fetch_array($adminsql);

normally it shouldn't make any differenze, but as said, i don't know the php mysql commandos very well..

mr e
02-13-2003, 09:27 PM
do it like this, and change whatever you want to repeat inside the while


$getadminonline = mysql_query("SELECT COUNT(userid) as total FROM session LEFT JOIN user USING(userid) WHERE user.usergroupid=6");

while ($adminonline=$DB_site->fetch_array($getadminonline)) {
if ($adminonline['total'] > 1) {
echo("There are $adminonline[total] admins online");
} elseif ($adminonline['total'] == 1) {
echo("There is $adminonline[total] admin online");
} else {
echo("There are no admins online");
}
}

Xenon
02-13-2003, 09:30 PM
@ mr e:
as long as $DB_site is not defined this can't work ;)