Welcome Guest [Log In] [Register]
Add Reply
Conservative Querying
Topic Started: Jan 27 2009, 05:01 PM (76 Views)
bustya
Member Avatar
The Master Bitchslapper
The less queries you use, the quicker your task will be accomplished. I've been going back through
all of my work and trying to reduce (and reuse) my queries as much as possible.
It should also be noted that when possible make the query itself do all the work rather
than SELECTING + altering a variable then UPDATING...

For instance, if there's some info you'll need about the current user from the usertable,
don't write a new query for it. You're already querying the usertable for the current
user's info, so in session.php in the checkLogin() function, add additional class variables
for the user.

Code: HTML
 

/* User is logged in, set class variables */
$this->userinfo = $database->getUserInfo($_SESSION['username']);
$this->username = $this->userinfo['username'];
$this->userid = $this->userinfo['userid'];
$this->userlevel = $this->userinfo['userlevel'];
$this->timezone = $this->userinfo['timezone'];
$this->avatar = $this->userinfo['avatar'];
$this->pic_count = $this->userinfo['pic_count'];
return true;


Then, just like with:

$user = $session->username;

You can:

$avatar = $session->avatar;


Now, I have a page that deletes an image. Since I allow users to designate any of their
pics as their avatar... even a cropped version of a pic, I have to check the usertable
for their avatar before I can delete the row in the image table and unset the image
from the image folder. I also have a limit of images per user built in, so there's a counter
in the usertable called pic_count. Since the user can only delete one image at a time,
this is easy WITHIN the query itself.

Code: HTML
 


// $picname is the image we're deleting...

$avatar = $session->avatar;
if (preg_match("/$picname/", $avatar)) {
$avatar = '';
}
else{
$avatar = $session->avatar;
}

mysql_query("UPDATE users SET `pic_count`=(`pic_count`-1), avatar='$avatar' WHERE username='$user'");


So, in the code above, I'm reusing the query the basic system uses to get info about
the current user and updating the current user's row in the usertable as a part of the
process to delete an image and it's row from the image table.
Edited by bustya, Jan 27 2009, 05:06 PM.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Other · Next Topic »
Add Reply