X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=db.php;h=f39d5e006afa1b259964be75c6c5b9fc488df6d8;hb=3faf64c6a1e0cbd4f880da951775c41fb8a6f0ab;hp=3e4f725c30758f0027a70afbf6dd6c15c06adff8;hpb=848c8a4d215f9cce1db0a7d37e733b5959b8210f;p=wfpl.git diff --git a/db.php b/db.php index 3e4f725..f39d5e0 100644 --- 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 . require_once('code/wfpl/encode.php'); @@ -111,7 +108,6 @@ function db_printf($str) { # This function does the work, but takes the parameters in an array function _db_printf($str, $args) { - $args = array_reverse($args); # because array_pop() takes from the end $out = ''; while($str) { $pos = strpos($str, '%'); @@ -129,11 +125,11 @@ function _db_printf($str, $args) { $str = substr($str, $pos + 2); if($chr == '"') { - $out .= '"' . enc_sql(array_pop($args)) . '"'; + $out .= '"' . enc_sql(array_shift($args)) . '"'; } elseif($chr == 's') { - $out .= enc_sql(array_pop($args)); + $out .= enc_sql(array_shift($args)); } elseif($chr == 'i') { - $int = format_int(array_pop($args)); + $int = format_int(array_shift($args)); if($int == '') $int = '0'; $out .= $int; } else { @@ -170,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); @@ -197,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,6 +237,12 @@ function db_get_value($table, $columns, $where = '') { return $value; } +function db_count($table, $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: # # db_insert('people', 'name,company', 'jason', 'widgets ltd');