00001 <?php
00002 # This file is part of the Savane project
00003 # <http://gna.org/projects/savane/>
00004 #
00005 # $Id: database.php 4567 2005-06-30 17:19:37Z toddy $
00006 # Copyright 1999-2000 (c) The SourceForge Crew
00007 #
00008 # 2004 (c) Elfyn McBratney <elfyn--at--emcb.co.uk>
00009 # Mathieu Roy <yeupou--gnu.org>
00010 #
00011 # The Savane project is free software; you can redistribute it and/or
00012 # modify it under the terms of the GNU General Public License
00013 # as published by the Free Software Foundation; either version 2
00014 # of the License, or (at your option) any later version.
00015 #
00016 # The Savane project is distributed in the hope that it will be useful,
00017 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 # GNU General Public License for more details.
00020 #
00021 # You should have received a copy of the GNU General Public License
00022 # along with the Savane project; if not, write to the Free Software
00023 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024
00025 function db_connect()
00026 {
00027 global $sys_dbhost,$sys_dbuser,$sys_dbpasswd,$conn;
00028
00029 $conn = @mysql_connect($sys_dbhost,$sys_dbuser,$sys_dbpasswd);
00030 if (!$conn) {
00031 fb("Failed to connect to database. Please contact as soon as possible server administrators. Until this problem get fixed, you will not be able to use this site.", 1);
00032 }
00033 }
00034
00035 function db_query($qstring,$print=0)
00036 {
00037
00038 # global $QUERY_COUNT;
00039 # $QUERY_COUNT++;
00040 if ($print) print "<br />Query is: $qstring<br />";
00041 # if ($GLOBALS[IS_DEBUG]) $GLOBALS[G_DEBUGQUERY] .= $qstring . "<BR>\n";
00042 global $sys_dbname;
00043 $GLOBALS['db_qhandle'] = @mysql($sys_dbname,$qstring);
00044 return $GLOBALS['db_qhandle'];
00045 }
00046
00047 function db_numrows($qhandle)
00048 {
00049 # return only if qhandle exists, otherwise 0
00050 if ($qhandle) {
00051 return @mysql_numrows($qhandle);
00052 } else {
00053 return 0;
00054 }
00055 }
00056
00057 function db_free_result($qhandle)
00058 {
00059 return @mysql_free_result($qhandle);
00060 }
00061
00062 function db_result($qhandle,$row,$field)
00063 {
00064 return @mysql_result($qhandle,$row,$field);
00065 }
00066
00067 function db_numfields($lhandle)
00068 {
00069 return @mysql_numfields($lhandle);
00070 }
00071
00072 function db_fieldname($lhandle,$fnumber)
00073 {
00074 return @mysql_field_name($lhandle,$fnumber);
00075 }
00076
00077 function db_affected_rows($qhandle)
00078 {
00079 return @mysql_affected_rows();
00080 }
00081
00082 function db_fetch_array($qhandle = 0)
00083 {
00084
00085 if ($qhandle) {
00086 return @mysql_fetch_array($qhandle);
00087 } else {
00088 if ($GLOBALS['db_qhandle']) {
00089 return @mysql_fetch_array($GLOBALS['db_qhandle']);
00090 } else {
00091 return (array());
00092 }
00093 }
00094 }
00095
00096 function db_insertid($qhandle)
00097 {
00098
00099 return @mysql_insert_id();
00100 }
00101
00102 function db_error()
00103 {
00104 return @mysql_error();
00105 }
00106
00107 # Return an sql insert command taking in input a qhandle:
00108 # it is supposed to ease copy a a row into another, ignoring the autoincrement
00109 # field + replacing another field value (like group_id)
00110 function db_createinsertinto ($result, $table, $row, $autoincrement_fieldname, $replace_fieldname='zxry', $replace_value='axa')
00111 {
00112 unset($fields,$values);
00113 for ($i = 0; $i < db_numfields($result); $i++)
00114 {
00115 $fieldname = db_fieldname($result, $i);
00116 # Create the sql by ignoring the autoincremental id
00117 if ($fieldname != $autoincrement_fieldname)
00118 {
00119 # If the value is empty
00120 if (db_result($result, $row, $fieldname) != NULL)
00121 {
00122
00123 if ($fields)
00124 {
00125 $fields .= ",";
00126 $values .= ",";
00127 }
00128
00129 $fields .= $fieldname;
00130 # Replace another field
00131 if ($fieldname == $replace_fieldname)
00132 {
00133 $values .= "'".$replace_value."'";
00134 }
00135 else
00136 { $values .= "'".db_result($result, $row, $fieldname)."'"; }
00137 }
00138 }
00139 }
00140 # No fields? Ignore
00141 if (!$fields)
00142 { return 0; }
00143
00144 return "INSERT INTO ".$table." ($fields) VALUES ($values)";
00145 }
00146
00147 ?>