PDA

View Full Version : Need help reducing queries


Dark Jim
02-18-2003, 11:24 PM
I made and submitted a hack "User Icons 1.1" just recently here but it needs quite a lot of queries on a few pages. For example it displays icons next to the user names on the who's online. The code in online.php looks like this currently:
$allusers= $DB_site->query("SELECT user.username, session.location, session.lastactivity, user.userid, user.usergroupid, user.invisible, session.host, user.showemail, user.receivepm
FROM session, user
". iif($WOLguests, " LEFT JOIN user USING (userid) ", ",user") ."
WHERE session.lastactivity > $datecut
". iif(!$WOLguests, " AND session.userid = user.userid", "") ."
ORDER BY user.username
");

$moderators = $DB_site->query("SELECT DISTINCT userid FROM moderator");
while ($mods = $DB_site->fetch_array($moderators)) {
$mod[$mods[userid]] = 1;
}

$count = 0;
while ($user = $DB_site->fetch_array($allusers)) {
if ($user['userid']) { // Reg'd Member
$key = $user['userid'];
if (($userinfo["$key"]['lastactivity'] < $user['lastactivity']) or !$userinfo["$key"]['lastactivity']) {
$userinfo["$key"]['realname'] = $user['username'];

$iconavatarurl=geticonavatarurl($user['userid']);
if ($iconavatarurl!="") {
$iconavatar = "<img src=\"$iconavatarurl\" border=\"0\" alt=\"\">";
} else {
$iconavatar = "";
}
if ($bbuserinfo[userid]>0 and !($bbuserinfo[showiconavatars])) {
$iconavatar = "";
}

if ($user['usergroupid'] == 6 and $highlightadmin) {
$userinfo["$key"]['username'] = "$iconavatar<font color={whosonlineowner}><b><i>$user[username]</i></b></font>";
Note the function "geticonavatarurl" which looks like this:
// ###################### Start geticonavatarurl #######################
function geticonavatarurl($userid) {
global $DB_site,$session;

if ($iconavatarinfo=$DB_site->query_first("SELECT user.iconavatarid,iconavatarpath,NOT ISNULL(iconavatardata) AS hascustom,customiconavatar.dateline
FROM user
LEFT JOIN iconavatar ON iconavatar.iconavatarid=user.iconavatarid
LEFT JOIN customiconavatar ON customiconavatar.userid=user.userid
WHERE user.userid='$userid'")) {
if ($iconavatarinfo[iconavatarpath]!="") {
return $iconavatarinfo[iconavatarpath];
} else if ($iconavatarinfo['hascustom']) {
return "iconavatar.php?userid=$userid&dateline=$iconavatarinfo[dateline]";
} else {
return '';
}
}
}
Is it possible to merge those 2 parts of code into 1 so it only needs 1 query to display the who's online instead of 1+ 1 for each icon. I tried some stuff myself but I'm really lost here.

Xenon
02-18-2003, 11:32 PM
try this:$allusers= $DB_site->query("SELECT user.username, user.iconavatarid,iconavatarpath,NOT ISNULL(iconavatardata) AS hascustom,customiconavatar.dateline,session.locati on, session.lastactivity, user.userid, user.usergroupid, user.invisible, session.host, user.showemail, user.receivepm
FROM session, user
LEFT JOIN iconavatar ON iconavatar.iconavatarid=user.iconavatarid
LEFT JOIN customiconavatar ON customiconavatar.userid=user.userid

". iif($WOLguests, " LEFT JOIN user USING (userid) ", ",user") ."
WHERE session.lastactivity > $datecut
". iif(!$WOLguests, " AND session.userid = user.userid", "") ."
ORDER BY user.username
");

$moderators = $DB_site->query("SELECT DISTINCT userid FROM moderator");
while ($mods = $DB_site->fetch_array($moderators)) {
$mod[$mods[userid]] = 1;
}

$count = 0;
while ($user = $DB_site->fetch_array($allusers)) {
if ($user['userid']) { // Reg'd Member
$key = $user['userid'];
if (($userinfo["$key"]['lastactivity'] < $user['lastactivity']) or !$userinfo["$key"]['lastactivity']) {
$userinfo["$key"]['realname'] = $user['username'];
if ($user[iconavatarpath]!="") {
$iconavatarurl=$user[iconavatarpath];
} else if ($user['hascustom']) {
$iconavatarurl="iconavatar.php?userid=$userid&dateline=$iconavatarinfo[dateline]";
} else {
$iconavatarurl='';
}
if ($iconavatarurl!="") {
$iconavatar = "<img src=\"$iconavatarurl\" border=\"0\" alt=\"\">";
} else {
$iconavatar = "";
}
if ($bbuserinfo[userid]>0 and !($bbuserinfo[showiconavatars])) {
$iconavatar = "";
}

if ($user['usergroupid'] == 6 and $highlightadmin) {
$userinfo["$key"]['username'] = "$iconavatar<font color={whosonlineowner}><b><i>$user[username]</i></b></font>";

Dark Jim
02-18-2003, 11:55 PM
Hmm I get a different error then when I tried something. This is what I get:
Database error in vBulletin 2.3.0:

Invalid SQL: SELECT user.username, user.iconavatarid,iconavatarpath,NOT ISNULL(iconavatardata) AS hascustom,customiconavatar.dateline,session.locati on, session.lastactivity, user.userid, user.usergroupid, user.invisible, session.host, user.showemail, user.receivepm
FROM session, user
LEFT JOIN iconavatar ON iconavatar.iconavatarid=user.iconavatarid
LEFT JOIN customiconavatar ON customiconavatar.userid=user.userid

LEFT JOIN user USING (userid)
WHERE session.lastactivity > 1045618523

ORDER BY user.username

mysql error: Not unique table/alias: 'user'

mysql error number: 1066

Xenon
02-19-2003, 10:54 AM
it's this line of your original script which pruduces the error:
". iif($WOLguests, " LEFT JOIN user USING (userid) ", ",user") ."

take it out..

Dark Jim
02-19-2003, 07:46 PM
Ack! That's no good. Suddenly all my members where online and all looking at the same forum. O_o Maybe I should add a query instead...

Xenon
02-19-2003, 09:18 PM
change this:
WHERE session.lastactivity > $datecut

into this:
WHERE session.userid = user.userid AND session.lastactivity > $datecut

Dark Jim
02-19-2003, 11:37 PM
Thanks that works after I also changed
$iconavatarurl="iconavatar.php?userid=$userid&dateline=$iconavatarinfo[dateline]"; to $iconavatarurl="iconavatar.php?userid=$user[userid]&dateline=$iconavatarinfo[dateline]";
Guests do not show up anymore though because of the removed line I assume. :/

Dark Jim
02-22-2003, 01:38 PM
I can't get it to show up guests. =/ Is it even possible with my hack included?