worlds most unnecessary loop

| December 29th, 2008

Found this bit in a model class of my own.

static public function active_product($product_type, $role_id) {
	$q = 'SELECT COUNT(*)
                FROM product
                WHERE
                    visible = 1 AND
                    (deleted = 0 OR archived = 0) AND
                    postedby_role_id = ? AND
                    product_type = ?';

        $sth = $dbh->prepare($q);
        $sth->execute($role_id, $product_type);

        $product_count = 0;
        while(list($count) = $sth->fetchrow_array()) {
            $product_count += $count;
        }

        return $product_count;
}

WTF was I smoking?

2 Responses to “worlds most unnecessary loop”

  1. evan Says:

    Well, you were just being thorough. Just in case MySQL didn’t want to count, your code was there to pick up the slack.

  2. jon Says:

    lol, sure?

Leave a Reply