JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
* template.php (tem->merge): only append expansions for show()n templates.
[wfpl.git] / db.php
diff --git a/db.php b/db.php
index 9390035..51452b8 100644 (file)
--- a/db.php
+++ b/db.php
@@ -2,21 +2,18 @@
 
 #  Copyright (C) 2006 Jason Woofenden
 #
-#  This file is part of wfpl.
-#
-#  wfpl is free software; you can redistribute it and/or modify it under the
-#  terms of the GNU Lesser General Public License as published by the Free
-#  Software Foundation; either version 2.1 of the License, or (at your option)
-#  any later version.
-#
-#  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
-#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-#  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
-#  more details.
-#
-#  You should have received a copy of the GNU Lesser General Public License
-#  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
-#  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+#  This program is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation, either version 3 of the License, or
+#  (at your option) any later version.
+#  
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#  
+#  You should have received a copy of the GNU General Public License
+#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
 require_once('code/wfpl/encode.php');
@@ -169,6 +166,22 @@ function db_get_rows($table, $columns, $where = '') {
        return $rows;
 }
 
+# like db_get_rows, but return array of hashes.
+function db_get_assocs($table, $columns, $where = '') {
+       $args = func_get_args();
+       $args = array_slice($args, 3);
+       $result = db_send_get($table, $columns, $where, $args);
+
+       $rows = array();
+       while($row = mysql_fetch_assoc($result)) {
+               $rows[] = $row;
+       }
+
+       mysql_free_result($result);
+
+       return $rows;
+}
+
 function db_get_column($table, $columns, $where = '') {
        $args = func_get_args();
        $args = array_slice($args, 3);
@@ -196,6 +209,19 @@ function db_get_row($table, $columns, $where = '') {
        return $row;
 }
 
+# like db_get_row, but return a hash.
+function db_get_assoc($table, $columns, $where = '') {
+       $args = func_get_args();
+       $args = array_slice($args, 3);
+       $result = db_send_get($table, $columns, $where, $args);
+
+       $row = mysql_fetch_assoc($result);
+
+       mysql_free_result($result);
+
+       return $row;
+}
+
 function db_get_value($table, $columns, $where = '') {
        $args = func_get_args();
        $args = array_slice($args, 3);
@@ -212,7 +238,9 @@ function db_get_value($table, $columns, $where = '') {
 }
 
 function db_count($table, $where = '') {
-       return db_get_value($table, 'count(*)', $where);
+       $args = func_get_args();
+       array_splice($args, 1, 0, array('count(*)'));
+       return call_user_func_array('db_get_value', $args);
 }
 
 # call either of these ways:
@@ -258,10 +286,10 @@ function db_insert_ish($command, $table, $columns, $values) {
        db_send_query($sql);
 }
 
-# to be consistant with the syntax of the other db functions, $values can be an
+# to be consistent with the syntax of the other db functions, $values can be an
 # array, a single value, or multiple parameters.
 #
-# as usual the where clause stuff is optional, but it will ofcourse update the
+# as usual the where clause stuff is optional, but it will of course update the
 # whole table if you leave it off.
 #
 # examples:
@@ -288,6 +316,7 @@ function db_update($table, $columns, $values) {
        $num_fields = count($columns);
 
        if(is_array($values)) {
+               $values = array_values($values);
                $args = array_slice($args, 1);
        } else {
                $values = array_slice($args, 0, $num_fields);
@@ -311,7 +340,7 @@ function db_update($table, $columns, $values) {
                $args = array_slice($args, 1);
 
                $sql .= ' ';
-               # any left for where claus arguments?
+               # any left for printf arguments?
                if($args) {
                        $sql .= _db_printf($where, $args);
                } else {