3 function sql_doconnect()
7 if( !strlen(trim( $dconf['dbhost'] )) ){
8 print "<h4>MySQL server not specified</h4>\n";
12 if( !strlen(trim( $dconf['dbuser'] )) ){
13 print "<h4>MySQL server username not specified</h4>\n";
17 if( $dconf['verbose'] ) print "<pre>\n";
20 // first try to connect to master server
23 $mysserv = $dconf['dbhost'];
24 $mysuser = $dconf['dbuser'];
25 $myspass = $dconf['dbpass'];
26 $mysdb = $dconf['dbname'];
31 if( $dconf['verbose'] )
32 print "Connecting to mysql://" . $mysuser . "@" . $mysserv . "/" . $mysdb . "...\n";
34 if( !strlen(trim( $myspass )) ){
35 $myc = mysql_connect( $mysserv , $mysuser );
37 $myc = mysql_connect( $mysserv , $mysuser , $myspass );
40 if( ! $myc ) print "<h4>Unable to connect to " . $mysserv . "</h4>\n";
42 if( !mysql_select_db( $mysdb , $myc ) ){
43 print "<h4>Could not select database " . $mysdb . "</h4>\n";
49 if( $myc ||
( $mysid > 1 ) ) break;
52 // try next secondary server
55 $mysserv = $dconf['dbsechost'];
56 $mysuser = $dconf['dbsecuser'];
57 $myspass = $dconf['dbsecpass'];
58 $mysdb = $dconf['dbsecname'];
62 if( $dconf['verbose'] ){
63 print "Connected to MySQL server " . mysql_get_server_info() . " at " . mysql_get_host_info() . "\n";
64 print "Selected database: " . $mysdb . "\n";
65 print "Client encoding: " . mysql_client_encoding() . "\n";
68 if( $dconf['verbose'] ) print "</pre>\n";
70 sql_setcharset( $myc , 'utf8' );
75 function sql_dodisconnect( $myc )
79 if( $dconf['verbose'] ) print "<pre>\n";
81 if( $dconf['verbose'] )
82 print "Disconnecting from " . $dconf['dbuser'] . "@" . $dconf['dbhost'] . "...\n";
86 print "<h4>Unable to disconnect from " . $dconf['dbhost'] . "</h4>\n";
90 if( $dconf['verbose'] ) print "</pre>\n";
95 function sql_setcharset( $myc , $cs )
99 $q = "SET NAMES '" . $cs . "'";
101 if( !mysql_query( $q , $myc ) ) {
102 print("Can't SET NAMES to " . $cs . "\n");
103 print mysql_error( $myc ) . "\n";
107 $q = "SET CHARACTER SET " . $cs;
109 if( !mysql_query( $q , $myc ) ) {
110 print("Can't SET CHARACTER SET to " . $cs . "\n");
111 print mysql_error( $myc ) . "\n";
115 if( $dconf['verbose'] ) print "<h4>MySQL: Character set to " . $cs . "</h4>\n";
120 function sql_emptytable( $myc , $tblname )
122 $q = "DELETE FROM " . $tblname;
124 if( !mysql_query( $q , $myc ) ) {
125 print("Can't empty table " . $tblname . " in " . $dconf['dbname'] . "\n");
126 print mysql_error( $myc ) . "\n";
130 print "<h4>MySQL: Table " . $tblname . " is now empty</h4>\n";
135 function sql_readtable( $myc , $tblname , $condition )
139 if( !strlen(trim( $tblname )) ){
140 print "<pre>Table name not specified</pre>\n";
144 $q = "SELECT * FROM " . $tblname;
145 if( strlen(trim($condition)) ) $q .= " WHERE " . $condition;
146 if( $debug ) dbg( "SQL: sql_readtable query" , $q );
148 $result = mysql_query( $q , $myc );
150 //print "<pre>" . ( $myc ) . "</pre>\n";
154 while ($row = mysql_fetch_assoc($result)) {
158 mysql_free_result($result);
160 if( isset( $tmpdb ) ) return $tmpdb;
164 function sql_gettable( $myc , $tblname , $condition )
166 return sql_readtable( $myc , $tblname , $condition );
169 function sql_getrecord( $myc , $tblname , $condition )
171 global $debug,$dconf;
173 if( !strlen(trim( $tblname )) ){
174 print "<pre>Table name not specified</pre>\n";
178 $q = "SELECT * FROM " . $tblname;
180 if( strlen(trim($condition)) ) $q .= " WHERE " . $condition;
181 if( $debug ) dbg( "SQL: sql_getrecord query" , $q );
183 $result = mysql_query( $q , $myc );
184 if( $debug ) dbg( "SQL: sql_getrecord result" , $result );
186 //print "<pre>" . ( $myc ) . "</pre>\n";
190 $row = mysql_fetch_assoc($result);
191 if( $debug ) dbg( "SQL: sql_getrecord row" , $row );
193 mysql_free_result($result);
198 function sql_listfields( $myc , $tblname )
200 global $debug,$dconf;
202 $result = mysql_query("SHOW COLUMNS FROM " . $tblname , $myc );
204 print mysql_error( $myc ) . "\n";
208 if( mysql_num_rows( $result ) > 0 ){
209 while( $row = mysql_fetch_assoc( $result ) ){
214 mysql_free_result($result);