From e2adfcf23a88f95e7c5f6611889665cf0782fe74 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Mon, 25 Dec 2006 02:16:10 -0500 Subject: [PATCH] added db.php --- db.php | 210 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 db.php diff --git a/db.php b/db.php new file mode 100644 index 0000000..89e972e --- /dev/null +++ b/db.php @@ -0,0 +1,210 @@ +\n"); + $result = mysql_query($sql); + if(!$result) { + die(enc_html('DATABASE ERROR: ' . mysql_error() . ' in the following query: ' . $sql)); + } + + return $result; +} + +# All select queries use this to generate the where clause, so they can work +# like printf. Currently three % codes are supported: +# +# %% put a % in the output +# %i put an integer in the output (strips non-numeric digits, and puts in 0 if blank) +# %" output double quotes, surrounding the variable which is encoded to be in there. +# %s output encoded to be in double quotes, but don't output the quotes +# +# complex example: db_get_rows('mytable', 'id', 'name=%" or company like "%%%s%%"', $name, $company_partial); + +function db_printf($str) { + $args = func_get_args(); + $args = array_slice($args, 1); + _db_printf($str, $args); +} + +# This function does the work, but takes the parameters in an array, and backwards. +function _db_printf($str, $args) { + $args = array_reverse($args); # because array_pop() takes from the end + $out = ''; + while($str) { + $pos = strpos($str, '%'); + if($pos === false) { # not found + # we hit the end. + return $out . $str; + } + # move everything up to (but not including) % to the output + $out .= substr($str, 0, $pos); + + # grab the character after the % + $chr = substr($str, $pos + 1, 1); + + # remove the stuff we've read from input + $str = substr($str, $pos + 2); + + if($chr == '"') { + $out .= '"' . enc_sql(array_pop($args)) . '"'; + } elseif($chr == 'i') { + $int = format_int(array_pop($args)); + if($int == '') $int = '0'; + $out .= $int; + } else { + $out .= $chr; + } + } + + return $out; +} + + +function db_send_get($table, $columns, $where = '', $args) { + $sql = "SELECT $columns FROM $table"; + if($where) { + $sql .= ' WHERE ' . _db_printf($where, $args); + } + + return db_send_query($sql); +} + + +function db_get_rows($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_row($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); + $result = db_send_get($table, $columns, $where, $args); + + $column = array(); + while($row = mysql_fetch_row($result)) { + $column[] = $row[0]; + } + + mysql_free_result($result); + + return $column; +} + +function db_get_row($table, $columns, $where = '') { + $args = func_get_args(); + $args = array_slice($args, 3); + $result = db_send_get($table, $columns, $where, $args); + + $row = mysql_fetch_row($result); + + mysql_free_result($result); + + return $row; +} + +function db_get_value($table, $columns, $where = '') { + $args = func_get_args(); + $args = array_slice($args, 3); + $result = db_send_get($table, $columns, $where, $args); + + $value = mysql_fetch_row($result); + if($value !== false) { + $value = $value[0]; + } + + mysql_free_result($result); + + return $value; +} + +?> -- 1.7.10.4