%s on line %d', pg_result_error($result), $sql, $backtrace[0]['file'], $backtrace[0]['line']); add_database_debug_trace ($sql, pg_result_error($result)); set_error_handler ('sql_error_handler'); trigger_error ($error); restore_error_handler (); return false; } else { $status = pg_result_status($result); $rows = pg_affected_rows($result); if ($status !== 2) { //The query NOT IS a select if ($rettype == "insert_id") { $result = postgresql_insert_id($dbconnection); } elseif ($rettype == "info") { $result = pg_result_status($result, PGSQL_STATUS_STRING); } else { $rows = pg_affected_rows($result); $result = $rows; } add_database_debug_trace ($sql, $result, $rows, array ('time' => $time)); return $result; } else { //The query IS a select. add_database_debug_trace ($sql, 0, $rows, array ('time' => $time)); while ($row = pg_fetch_assoc($result)) { array_push ($retval, $row); } if ($cache === true) $sql_cache[$sql] = $retval; pg_free_result ($result); } } } if (! empty ($retval)) { return $retval; } //Return false, check with === or !== return false; } /** * Get all the rows in a table of the database. * * @param string Database table name. * @param string Field to order by. * @param string $order The type of order, by default 'ASC'. * * @return mixed A matrix with all the values in the table */ function postgresql_get_db_all_rows_in_table($table, $order_field = "", $order = 'ASC') { if ($order_field != "") { return get_db_all_rows_sql ('SELECT * FROM "'.$table.'" ORDER BY "'.$order_field . ' ' . $order); } else { return get_db_all_rows_sql ('SELECT * FROM "'.$table.'"'); } } /** * Inserts strings into database * * The number of values should be the same or a positive integer multiple as the number of rows * If you have an associate array (eg. array ("row1" => "value1")) you can use this function with ($table, array_keys ($array), $array) in it's options * All arrays and values should have been cleaned before passing. It's not neccessary to add quotes. * * @param string Table to insert into * @param mixed A single value or array of values to insert (can be a multiple amount of rows) * * @return mixed False in case of error or invalid values passed. Affected rows otherwise */ function postgresql_process_sql_insert($table, $values) { //Empty rows or values not processed if (empty ($values)) return false; $values = (array) $values; $query = sprintf ('INSERT INTO "%s" ', $table); $fields = array (); $values_str = ''; $i = 1; $max = count ($values); foreach ($values as $field => $value) { //Add the correct escaping to values if ($field[0] != '"') { $field = '"' . $field . '"'; } array_push ($fields, $field); if (is_null ($value)) { $values_str .= "NULL"; } elseif (is_int ($value) || is_bool ($value)) { $values_str .= sprintf("%d", $value); } else if (is_float ($value) || is_double ($value)) { $values_str .= sprintf("%f", $value); } else { $values_str .= sprintf("'%s'", $value); } if ($i < $max) { $values_str .= ","; } $i++; } $query .= '(' . implode(', ', $fields) . ')'; $query .= ' VALUES (' . $values_str . ')'; return process_sql($query, 'insert_id'); } /** * * Escape string to set it properly to use in sql queries * * @param string String to be cleaned. * * @return string String cleaned. */ function postgresql_escape_string_sql($string) { $str = pg_escape_string($string); return $str; } ?>