TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Class desconstruct/available resources (http://www.talkphp.com/general/2566-class-desconstruct-available-resources.html)

delayedinsanity 04-02-2008 02:29 AM

Class desconstruct/available resources
 
I'm sure everybody will say yes to this, but... if I have a class that opens a connection to my database when its called, should I worry too much about setting up a __destruct that closes it and unset() at the end of every page? The page has already loaded, accessed all its functions and displayed everything... there's no open connections after the person leaves the page anyways, right?

delayedinsanity 04-02-2008 02:49 AM

Another question, not really relating, but kind of - I'm building a small customer support system for my site, and at the top of each administrative page I list the total posts, total new posts, and total posts awaiting reply (read but not replied).

As of now I have three seperate functions, dbTotalPosts(), dbNewPosts(), dbWaitingPosts(), each doing their own database query (SELECT ticket FROM contact, SELECT ticket FROM contact WHERE new='1', etc). All of them use the same db connection, they're not opening and closing it, so is this okay, or would it be better to do one query (SELECT new, replied FROM contact) and then loop through the results and provide a $count of each row? ($total++, $new++, $replied++, or I guess just $new++, $replied++ $total = $new + $replied)

Thanks in advance,
-m

delayedinsanity 04-02-2008 04:31 AM

The Code
 
First question is still my most important, but here's the code I just wrote quick to reduce the number of database calls from 3 to 1.

PHP Code:

// dbPostCount - counts total posts, new posts, and posts awaiting response
function dbPostCount() {
    
$query "SELECT new, replied FROM " TBL_CONTACT;
    
$result mysql_query($query$this->connection);
    
    while (
$row mysql_fetch_row($result))
    {
        
$c['total']++;
        if (
$row[0] == "1") { $c['new']++; } elseif ($row[0] == "0" && $row[1] == "0") { $c['waiting']++; }
    }
    
    return 
$c;


Can I improve on this? I'm willing to bet just from the 3 to 1 database calls that it IS quicker than my original method.
-m


All times are GMT. The time now is 08:28 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0