SF.net SVN: postfixadmin: [171] trunk/functions.inc.php
Brought to you by:
christian_boltz,
gingerdog
|
From: <chr...@us...> - 2007-11-01 23:58:13
|
Revision: 171
http://postfixadmin.svn.sourceforge.net/postfixadmin/?rev=171&view=rev
Author: christian_boltz
Date: 2007-11-01 16:58:12 -0700 (Thu, 01 Nov 2007)
Log Message:
-----------
- added function db_insert()
- added optional $ignore_errors parameter to db_query()
- db_query() now returns 'error' as additional key in the result array
- renamed $setup to $ignore_errors in db_connect()
- added fallback to $table_key to table_by_key()
Modified Paths:
--------------
trunk/functions.inc.php
Modified: trunk/functions.inc.php
===================================================================
--- trunk/functions.inc.php 2007-10-31 20:18:53 UTC (rev 170)
+++ trunk/functions.inc.php 2007-11-01 23:58:12 UTC (rev 171)
@@ -1284,20 +1284,20 @@
* db_connect
* Action: Makes a connection to the database if it doesn't exist
* Call: db_connect ()
- * Optional parameter: $setup = TRUE, used by setup.php
+ * Optional parameter: $ignore_errors = TRUE, used by setup.php
*
* Return value:
- * a) without $setup or $setup == 0
+ * a) without $ignore_errors or $ignore_errors == 0
* - $link - the database connection -OR-
* - call die() in case of connection problems
- * b) with $setup == TRUE
+ * b) with $ignore_errors == TRUE
* array($link, $error_text);
*/
-function db_connect ($setup = 0)
+function db_connect ($ignore_errors = 0)
{
global $CONF;
global $DEBUG_TEXT;
- if ($setup != 0) $DEBUG_TEXT = '';
+ if ($ignore_errors != 0) $DEBUG_TEXT = '';
$error_text = '';
$link = 0;
@@ -1351,7 +1351,7 @@
$error_text = "<p />DEBUG INFORMATION:<br />Invalid \$CONF['database_type']! Please fix your config.inc.php! $DEBUG_TEXT";
}
- if ($setup)
+ if ($ignore_errors)
{
return array($link, $error_text);
}
@@ -1406,43 +1406,53 @@
// db_query
// Action: Sends a query to the database and returns query result and number of rows
// Call: db_query (string query)
+// Optional parameter: $ignore_errors = TRUE, used by upgrade.php
//
-function db_query ($query)
+function db_query ($query, $ignore_errors = 0)
{
global $CONF;
global $DEBUG_TEXT;
$result = "";
$number_rows = "";
static $link;
+ $error_text = "";
+ if ($ignore_errors) $DEBUG_TEXT = "";
if (!is_resource($link)) $link = db_connect ();
- if ($CONF['database_type'] == "mysql") $result = @mysql_query ($query, $link) or die ("<p />DEBUG INFORMATION:<br />Invalid query: " . mysql_error($link) . "$DEBUG_TEXT");
- if ($CONF['database_type'] == "mysqli") $result = @mysqli_query ($link, $query) or die ("<p />DEBUG INFORMATION:<br />Invalid query: " . mysqli_error($link) . "$DEBUG_TEXT");
+ if ($CONF['database_type'] == "mysql") $result = @mysql_query ($query, $link)
+ or $error_text = "<p />DEBUG INFORMATION:<br />Invalid query: " . mysql_error($link) . "$DEBUG_TEXT";
+ if ($CONF['database_type'] == "mysqli") $result = @mysqli_query ($link, $query)
+ or $error_text = "<p />DEBUG INFORMATION:<br />Invalid query: " . mysqli_error($link) . "$DEBUG_TEXT";
if ($CONF['database_type'] == "pgsql")
{
- $result = @pg_query ($link, $query) or die ("<p />DEBUG INFORMATION:<br />Invalid query: " . pg_last_error() . "$DEBUG_TEXT");
+ $result = @pg_query ($link, $query)
+ or $error_text = "<p />DEBUG INFORMATION:<br />Invalid query: " . pg_last_error() . "$DEBUG_TEXT";
}
+ if ($error_text != "" && $ignore_errors == 0) die($error_text);
- if (eregi ("^SELECT", $query))
- {
- // if $query was a SELECT statement check the number of rows with [database_type]_num_rows ().
- if ($CONF['database_type'] == "mysql") $number_rows = mysql_num_rows ($result);
- if ($CONF['database_type'] == "mysqli") $number_rows = mysqli_num_rows ($result);
- if ($CONF['database_type'] == "pgsql") $number_rows = pg_num_rows ($result);
+ if ($error_text == "") {
+ if (eregi ("^SELECT", $query))
+ {
+ // if $query was a SELECT statement check the number of rows with [database_type]_num_rows ().
+ if ($CONF['database_type'] == "mysql") $number_rows = mysql_num_rows ($result);
+ if ($CONF['database_type'] == "mysqli") $number_rows = mysqli_num_rows ($result);
+ if ($CONF['database_type'] == "pgsql") $number_rows = pg_num_rows ($result);
+ }
+ else
+ {
+ // if $query was something else, UPDATE, DELETE or INSERT check the number of rows with
+ // [database_type]_affected_rows ().
+ if ($CONF['database_type'] == "mysql") $number_rows = mysql_affected_rows ($link);
+ if ($CONF['database_type'] == "mysqli") $number_rows = mysqli_affected_rows ($link);
+ if ($CONF['database_type'] == "pgsql") $number_rows = pg_affected_rows ($result);
+ }
}
- else
- {
- // if $query was something else, UPDATE, DELETE or INSERT check the number of rows with
- // [database_type]_affected_rows ().
- if ($CONF['database_type'] == "mysql") $number_rows = mysql_affected_rows ($link);
- if ($CONF['database_type'] == "mysqli") $number_rows = mysqli_affected_rows ($link);
- if ($CONF['database_type'] == "pgsql") $number_rows = pg_affected_rows ($result);
- }
$return = array (
"result" => $result,
- "rows" => $number_rows
+ "rows" => $number_rows,
+ "error" => $error_text
);
return $return;
}
@@ -1516,7 +1526,29 @@
}
+//
+// db_insert
+// Action: Inserts a row from a specified table
+// Call: db_insert (string table, array values)
+//
+function db_insert ($table, $values)
+{
+ $sql_values = "(" . implode(",",escape_string(array_keys($values))).") VALUES ('".implode("','",escape_string($values))."')";
+ $table = table_by_key ($table);
+ $result = db_query ("INSERT INTO $table $sql_values");
+ if ($result['rows'] >= 1)
+ {
+ return $result['rows'];
+ }
+ else
+ {
+ return true;
+ }
+}
+
+
+
//
// db_log
// Action: Logs actions from admin
@@ -1571,6 +1603,7 @@
{
global $CONF;
$table = $CONF['database_prefix'].$CONF['database_tables'][$table_key];
+ if (empty($table)) $table = $table_key;
return $table;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|