TVDB: better handling of first run
[nonametv.git] / web-admin / mysql.php
blob9a0eacce6650761c2e7fcbd3c2c36b0089071186
1 <?php
3 function sql_doconnect()
5 global $dconf;
7 if( !strlen(trim( $dconf['dbhost'] )) ){
8 print "<h4>MySQL server not specified</h4>\n";
9 return false;
12 if( !strlen(trim( $dconf['dbuser'] )) ){
13 print "<h4>MySQL server username not specified</h4>\n";
14 return false;
17 if( $dconf['verbose'] ) print "<pre>\n";
20 // first try to connect to master server
22 $mysid = 1;
23 $mysserv = $dconf['dbhost'];
24 $mysuser = $dconf['dbuser'];
25 $myspass = $dconf['dbpass'];
26 $mysdb = $dconf['dbname'];
28 $myc = false;
30 while( true ){
31 if( $dconf['verbose'] )
32 print "Connecting to mysql://" . $mysuser . "@" . $mysserv . "/" . $mysdb . "...\n";
34 if( !strlen(trim( $myspass )) ){
35 $myc = mysql_connect( $mysserv , $mysuser );
36 } else {
37 $myc = mysql_connect( $mysserv , $mysuser , $myspass );
40 if( ! $myc ) print "<h4>Unable to connect to " . $mysserv . "</h4>\n";
41 else {
42 if( !mysql_select_db( $mysdb , $myc ) ){
43 print "<h4>Could not select database " . $mysdb . "</h4>\n";
47 flush();
49 if( $myc || ( $mysid > 1 ) ) break;
50 else {
52 // try next secondary server
54 $mysid = 2;
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' );
72 return $myc;
75 function sql_dodisconnect( $myc )
77 global $dconf;
79 if( $dconf['verbose'] ) print "<pre>\n";
81 if( $dconf['verbose'] )
82 print "Disconnecting from " . $dconf['dbuser'] . "@" . $dconf['dbhost'] . "...\n";
84 mysql_close ( $myc );
85 if( ! $myc ){
86 print "<h4>Unable to disconnect from " . $dconf['dbhost'] . "</h4>\n";
87 return false;
90 if( $dconf['verbose'] ) print "</pre>\n";
92 return true;
95 function sql_setcharset( $myc , $cs )
97 global $dconf;
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";
104 return false;
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";
112 return false;
115 if( $dconf['verbose'] ) print "<h4>MySQL: Character set to " . $cs . "</h4>\n";
117 return true;
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";
127 return false;
130 print "<h4>MySQL: Table " . $tblname . " is now empty</h4>\n";
132 return true;
135 function sql_readtable( $myc , $tblname , $condition )
137 global $debug;
139 if( !strlen(trim( $tblname )) ){
140 print "<pre>Table name not specified</pre>\n";
141 return false;
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 );
149 if( ! $result ){
150 //print "<pre>" . ( $myc ) . "</pre>\n";
151 return false;
154 while ($row = mysql_fetch_assoc($result)) {
155 $tmpdb[] = $row;
158 mysql_free_result($result);
160 if( isset( $tmpdb ) ) return $tmpdb;
161 else return false;
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";
175 return false;
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 );
185 if( ! $result ){
186 //print "<pre>" . ( $myc ) . "</pre>\n";
187 return false;
190 $row = mysql_fetch_assoc($result);
191 if( $debug ) dbg( "SQL: sql_getrecord row" , $row );
193 mysql_free_result($result);
195 return $row;
198 function sql_listfields( $myc , $tblname )
200 global $debug,$dconf;
202 $result = mysql_query("SHOW COLUMNS FROM " . $tblname , $myc );
203 if( ! $result ){
204 print mysql_error( $myc ) . "\n";
205 return false;
208 if( mysql_num_rows( $result ) > 0 ){
209 while( $row = mysql_fetch_assoc( $result ) ){
210 $fields[] = $row;
214 mysql_free_result($result);
216 return( $fields );