| | 1 | <?php |
| | 2 | |
| | 3 | // ORIGINAL CODE FROM: |
| | 4 | // Justin Vincent (justin@visunet.ie) |
| | 5 | // http://php.justinvincent.com |
| | 6 | |
| | 7 | |
| | 8 | define("EZSQL_VERSION","1.26"); |
| | 9 | define("OBJECT","OBJECT",true); |
| | 10 | define("ARRAY_A","ARRAY_A",true); |
| | 11 | define("ARRAY_N","ARRAY_N",true); |
| | 12 | |
| | 13 | if (!defined('SAVEQUERIES')) |
| | 14 | define('SAVEQUERIES', false); |
| | 15 | |
| | 16 | // ================================================================== |
| | 17 | // The Main Class |
| | 18 | |
| | 19 | class wpdb { |
| | 20 | |
| | 21 | var $show_errors = true; |
| | 22 | var $num_queries = 0; |
| | 23 | var $last_query; |
| | 24 | var $col_info; |
| | 25 | var $queries; |
| | 26 | |
| | 27 | // Our tables |
| | 28 | var $posts; |
| | 29 | var $users; |
| | 30 | var $categories; |
| | 31 | var $post2cat; |
| | 32 | var $comments; |
| | 33 | var $links; |
| | 34 | var $linkcategories; |
| | 35 | var $options; |
| | 36 | var $optiontypes; |
| | 37 | var $optionvalues; |
| | 38 | var $optiongroups; |
| | 39 | var $optiongroup_options; |
| | 40 | var $postmeta; |
| | 41 | |
| | 42 | // ================================================================== |
| | 43 | // DB Constructor - connects to the server and selects a database |
| | 44 | |
| | 45 | function wpdb($dbpath, $dbname) { |
| | 46 | $this->dbh = sqlite_open($dbpath.$dbname); |
| | 47 | if ( ! $this->dbh ) { |
| | 48 | $this->print_error("Error","<ol><b>Error establishing a database!</b><li>Are you sure you have the correct path?<li>Are you sure that you have typed the correct database instance name?<li>Are you sure that the database is installed?</ol>"); |
| | 49 | } |
| | 50 | |
| | 51 | sqlite_query($this->dbh, 'PRAGMA short_column_names = 1'); |
| | 52 | |
| | 53 | // User defined functions to use in SQLite |
| | 54 | sqlite_create_function($this->dbh, 'trim', 'trim', 1); |
| | 55 | |
| | 56 | } |
| | 57 | |
| | 58 | // ================================================================== |
| | 59 | // Select a DB instance (if another one needs to be selected) |
| | 60 | |
| | 61 | function select($dbpath, $dbname) { |
| | 62 | $this->wpdb($dbpath, $dbname); |
| | 63 | } |
| | 64 | |
| | 65 | // ==================================================================== |
| | 66 | // Format a string correctly for safe insert under all PHP conditions |
| | 67 | |
| | 68 | function escape($str) { |
| | 69 | return str_replace("'","''",str_replace("''","'",stripslashes($str))); |
| | 70 | } |
| | 71 | |
| | 72 | // ================================================================== |
| | 73 | // Print SQL/DB error. |
| | 74 | |
| | 75 | function print_error($title = "SQL/DB Error", $str = "") { |
| | 76 | |
| | 77 | // All erros go to the global error array $EZSQL_ERROR.. |
| | 78 | global $EZSQL_ERROR; |
| | 79 | |
| | 80 | // If no error string then you're SOL |
| | 81 | if ( !$str ) { |
| | 82 | $str = "An error occured, quick go tell someone!"; |
| | 83 | } |
| | 84 | |
| | 85 | // Log this error to the global array.. |
| | 86 | $EZSQL_ERROR[] = array |
| | 87 | ( |
| | 88 | "query" => $this->last_query, |
| | 89 | "error_str" => $str |
| | 90 | ); |
| | 91 | |
| | 92 | // Is error output turned on or not.. |
| | 93 | if ( $this->show_errors ) { |
| | 94 | // If there is an error then take note of it |
| | 95 | print "<div id='error'> |
| | 96 | <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> |
| | 97 | <code>$this->last_query</code></p> |
| | 98 | </div>"; |
| | 99 | } |
| | 100 | else { |
| | 101 | return false; |
| | 102 | } |
| | 103 | } |
| | 104 | |
| | 105 | // ================================================================== |
| | 106 | // Turn error handling on or off.. |
| | 107 | |
| | 108 | function show_errors() { |
| | 109 | $this->show_errors = true; |
| | 110 | } |
| | 111 | |
| | 112 | function hide_errors() { |
| | 113 | $this->show_errors = false; |
| | 114 | } |
| | 115 | |
| | 116 | // ================================================================== |
| | 117 | // Kill cached query results |
| | 118 | |
| | 119 | function flush() { |
| | 120 | |
| | 121 | // Get rid of these |
| | 122 | $this->last_result = null; |
| | 123 | $this->col_info = null; |
| | 124 | $this->last_query = null; |
| | 125 | |
| | 126 | } |
| | 127 | |
| | 128 | // ================================================================== |
| | 129 | // Basic Query - see docs for more detail |
| | 130 | |
| | 131 | function query($query) { |
| | 132 | |
| | 133 | // For reg expressions |
| | 134 | $query = trim($query); |
| | 135 | |
| | 136 | // Flush cached values.. |
| | 137 | $this->flush(); |
| | 138 | |
| | 139 | // Parse query to be used in SQLite |
| | 140 | $query = $this->parse($query); |
| | 141 | |
| | 142 | // Log how the function was called |
| | 143 | $this->func_call = "\$wpdb->query(\"$query\")"; |
| | 144 | |
| | 145 | // Keep track of the last query for debug.. |
| | 146 | $this->last_query = $query; |
| | 147 | |
| | 148 | if (SAVEQUERIES) |
| | 149 | $this->timer_start(); |
| | 150 | |
| | 151 | $handle = @sqlite_query($this->dbh,$query); |
| | 152 | |
| | 153 | |
| | 154 | if (SAVEQUERIES) |
| | 155 | $this->queries[] = array( $query, $this->timer_stop() ); |
| | 156 | |
| | 157 | // If there was an insert, delete or update see how many rows were affected |
| | 158 | // (Also, If there there was an insert take note of the insert_id |
| | 159 | $query_type = array("insert","delete","update","replace"); |
| | 160 | |
| | 161 | // loop through the above array |
| | 162 | foreach ( $query_type as $word ) { |
| | 163 | // This is true if the query starts with insert, delete or update |
| | 164 | if ( preg_match("/^$word\s+/i",$query) ) { |
| | 165 | $this->rows_affected = sqlite_changes($this->dbh); |
| | 166 | |
| | 167 | // This gets the insert ID |
| | 168 | if ( $word == "insert" || $word == "replace" ) { |
| | 169 | $this->insert_id = sqlite_last_insert_rowid($this->dbh); |
| | 170 | |
| | 171 | // If insert id then return it - true evaluation |
| | 172 | return $this->insert_id; |
| | 173 | } |
| | 174 | |
| | 175 | // Set to false if there was no insert id |
| | 176 | $this->result = false; |
| | 177 | |
| | 178 | } |
| | 179 | |
| | 180 | } |
| | 181 | |
| | 182 | if ( ! $handle ) { |
| | 183 | |
| | 184 | // If there is an error then take note of it.. |
| | 185 | if ( $php_errormsg ) { |
| | 186 | $this->print_error($php_errormsg,$query); |
| | 187 | } else { |
| | 188 | $this->print_error("Invalid Query",$query); |
| | 189 | } |
| | 190 | |
| | 191 | } else { |
| | 192 | |
| | 193 | |
| | 194 | $i=0; |
| | 195 | while ( $row = sqlite_fetch_object($handle) ) { |
| | 196 | $this->last_result[$i++] = $row; |
| | 197 | } |
| | 198 | |
| | 199 | // Log number of rows the query returned |
| | 200 | $this->num_rows = $i; |
| | 201 | |
| | 202 | // If debug ALL queries |
| | 203 | $this->debug_all ? $this->debug() : null ; |
| | 204 | |
| | 205 | // If there were results then return true for $db->query |
| | 206 | if ( $i ) { |
| | 207 | return true; |
| | 208 | } else { |
| | 209 | return false; |
| | 210 | } |
| | 211 | |
| | 212 | } |
| | 213 | } |
| | 214 | |
| | 215 | // ================================================================== |
| | 216 | // Parse statement and make it SQLite friendly |
| | 217 | // original code from tikiwiki project |
| | 218 | |
| | 219 | function parse($stmt) { |
| | 220 | |
| | 221 | // variable for statements that have to be appended |
| | 222 | global $poststmt; |
| | 223 | $poststmt = "\n\n"; |
| | 224 | |
| | 225 | // SQLite don't support 'SHOW TABLES' |
| | 226 | $stmt = preg_replace("/SHOW TABLES/", "SELECT name FROM sqlite_master WHERE type='table'", $stmt); |
| | 227 | |
| | 228 | |
| | 229 | $stmt = preg_replace("/COUNT[(]DISTINCT([^)]*)[)] FROM ([^'']*) (.*)/","COUNT(*) FROM (SELECT DISTINCT $1 FROM $2 $3)",$stmt); |
| | 230 | |
| | 231 | // Date and time functions |
| | 232 | $stmt = preg_replace("/YEAR[(]([^)]*)[)]/","strftime(\"%Y\", $1)",$stmt); |
| | 233 | $stmt = preg_replace("/MONTH[(]([^)]*)[)]/","strftime(\"%m\", $1)",$stmt); |
| | 234 | $stmt = preg_replace("/DAY[(]([^)]*)[)]/","strftime(\"%d\", $1)",$stmt); |
| | 235 | $stmt = preg_replace("/NOW[(][)]/","date('now')",$stmt); |
| | 236 | $stmt = preg_replace("/INTERVAL ([0-9]*)([^)]*)/","\"+$1 $2\"",$stmt); |
| | 237 | $stmt = preg_replace("/DATE_ADD[(]([^,]*),([^)]*)[)]/","datetime($1, $2)",$stmt); |
| | 238 | $stmt = preg_replace("/UNIX_TIMESTAMP[(]([^)]*)[)]/","strftime('%s',$1)",$stmt); |
| | 239 | |
| | 240 | $stmt = preg_replace("/[`]/","'",$stmt); |
| | 241 | |
| | 242 | // MD5 |
| | 243 | $stmt = preg_replace("/MD5[(][']([^']*)['][)]/e", "'\''.md5('$1').'\''" ,$stmt); |
| | 244 | |
| | 245 | // Enum |
| | 246 | $stmt = preg_replace("/enum[(][^)]*[)]/","varchar(255)",$stmt); |
| | 247 | |
| | 248 | // Random function |
| | 249 | $stmt = preg_replace("/rand[(][)]/","random()",$stmt); |
| | 250 | |
| | 251 | // IF statement ##FIXME not a good way to do this |
| | 252 | $stmt = preg_replace("/IF [(](.*)[,] [0-1][,][0-1][)]/","$1",$stmt); |
| | 253 | |
| | 254 | //replace comments |
| | 255 | $stmt = preg_replace("/#/","--",$stmt); |
| | 256 | |
| | 257 | $stmt = preg_replace("/TYPE=MyISAM/","",$stmt); |
| | 258 | //$stmt = preg_replace("/AUTO_INCREMENT=1/","",$stmt); |
| | 259 | |
| | 260 | //postgres cannot DROP TABLE IF EXISTS ( sqlite?? ) |
| | 261 | $stmt = preg_replace("/DROP TABLE IF EXISTS/","DROP TABLE",$stmt); |
| | 262 | |
| | 263 | //auto_increment things |
| | 264 | $stmt = preg_replace("/(big)int\(.\) (unsigned )*NOT NULL auto_increment/","INTEGER",$stmt); |
| | 265 | $stmt = preg_replace("/(big)int\(..\) (unsigned )*NOT NULL auto_increment/","INTEGER",$stmt); |
| | 266 | |
| | 267 | // integer types |
| | 268 | $stmt = preg_replace("/tinyint\([1-4]\)/","smallint",$stmt); |
| | 269 | $stmt = preg_replace("/int\([1-4]\)/","smallint",$stmt); |
| | 270 | $stmt = preg_replace("/int\([5-9]\)/","integer",$stmt); |
| | 271 | $stmt = preg_replace("/int\(..\)/","bigint",$stmt); |
| | 272 | |
| | 273 | // timestamps |
| | 274 | $stmt = preg_replace("/timestamp\([^\)]+\)/","timestamp(3)",$stmt); |
| | 275 | |
| | 276 | // blobs |
| | 277 | $stmt = preg_replace("/longblob|tinyblob|blob/","bytea",$stmt); |
| | 278 | |
| | 279 | // ##FIXME quote column names, not sure if needed... |
| | 280 | //$stmt = preg_replace("/\n[ \t]+([a-zA-Z0-9_]+)/","\n \"$1\"",$stmt); |
| | 281 | |
| | 282 | // quote and record table names |
| | 283 | $stmt = preg_replace("/(DROP TABLE |CREATE TABLE )([a-zA-Z0-9_]+)( \()*/e","record_tablename('$1','$2','$3')",$stmt); |
| | 284 | |
| | 285 | // unquote the PRIMARY and other Keys |
| | 286 | $stmt = preg_replace("/\n[ \t]+\"(PRIMARY|KEY|FULLTEXT|UNIQUE)\"/","\n $1",$stmt); |
| | 287 | |
| | 288 | // convert enums |
| | 289 | $stmt = preg_replace("/\n[ \t]+(\"[a-zA-Z0-9_]+\") enum\(([^\)]+)\)/e","convert_enums('$1','$2')",$stmt); |
| | 290 | // quote column names in primary keys |
| | 291 | $stmt = preg_replace("/\n[ \t]+(PRIMARY KEY) \((.+)\),*/e","quote_prim_cols('$1','$2')",$stmt); |
| | 292 | |
| | 293 | // create indexes from KEY ... |
| | 294 | $stmt = preg_replace("/\n[ \t]+KEY ([a-zA-Z0-9_]+) \((.+)\),*/e","create_index('$1','$2')",$stmt); |
| | 295 | $stmt = preg_replace("/\n[ \t]+FULLTEXT KEY ([a-zA-Z0-9_]+) \((.+)\),*/e","create_index('$1','$2')",$stmt); |
| | 296 | $stmt = preg_replace("/\n[ \t]+(UNIQUE) KEY ([a-zA-Z0-9_]+) \((.+)\),*/e","create_index('$2','$3','$1')",$stmt); |
| | 297 | |
| | 298 | // handle inserts |
| | 299 | $stmt = preg_replace("/INSERT INTO ([a-zA-Z0-9_]*).*\(([^\)]+)\) VALUES (.*)/e","do_inserts('$1','$2','$3')",$stmt); |
| | 300 | $stmt = preg_replace("/INSERT IGNORE INTO ([a-zA-Z0-9_]*).*\(([^\)]+)\) VALUES (.*)/e","do_inserts('$1','$2','$3')", $stmt); |
| | 301 | |
| | 302 | // why does i modifier not work??? |
| | 303 | $stmt = preg_replace("/insert into ([a-zA-Z0-9_]*).*\(([^\)]+)\) values(.*)/e","do_inserts('$1','$2','$3')",$stmt); |
| | 304 | |
| | 305 | // the update |
| | 306 | $stmt = preg_replace("/update ([a-zA-Z0-9_]+) set (.*)/e","do_updates('$1','$2')",$stmt); |
| | 307 | $stmt = preg_replace("/UPDATE ([a-zA-Z0-9_]+) set (.*)/e","do_updates('$1','$2')",$stmt); |
| | 308 | |
| | 309 | // Ignore 'IGNORE' |
| | 310 | $stmt = preg_replace("/IGNORE/","",$stmt); |
| | 311 | |
| | 312 | // clean cases where UNIQUE was alone at the end |
| | 313 | $stmt = preg_replace("/,(\s*)\)/","$1)",$stmt); |
| | 314 | return $stmt.";".$poststmt; |
| | 315 | } |
| | 316 | |
| | 317 | |
| | 318 | |
| | 319 | // ================================================================== |
| | 320 | // Get one variable from the DB - see docs for more detail |
| | 321 | |
| | 322 | function get_var($query=null,$x=0,$y=0) { |
| | 323 | |
| | 324 | // Log how the function was called |
| | 325 | $this->func_call = "\$wpdb->get_var(\"$query\",$x,$y)"; |
| | 326 | |
| | 327 | // If there is a query then perform it if not then use cached results.. |
| | 328 | if ( $query ) { |
| | 329 | $this->query($query); |
| | 330 | } |
| | 331 | |
| | 332 | // Extract var out of cached results based x,y vals |
| | 333 | if ( $this->last_result[$y] ) { |
| | 334 | $values = array_values(get_object_vars($this->last_result[$y])); |
| | 335 | } |
| | 336 | |
| | 337 | // If there is a value return it else return null |
| | 338 | return (isset($values[$x]) && $values[$x]!=='')?$values[$x]:null; |
| | 339 | } |
| | 340 | |
| | 341 | // ================================================================== |
| | 342 | // Get one row from the DB - see docs for more detail |
| | 343 | |
| | 344 | function get_row($query=null,$output=OBJECT,$y=0) { |
| | 345 | |
| | 346 | // Log how the function was called |
| | 347 | $this->func_call = "\$wpdb->get_row(\"$query\",$output,$y)"; |
| | 348 | |
| | 349 | // If there is a query then perform it if not then use cached results.. |
| | 350 | if ( $query ) { |
| | 351 | $this->query($query); |
| | 352 | } |
| | 353 | |
| | 354 | // If the output is an object then return object using the row offset.. |
| | 355 | if ( $output == OBJECT ) { |
| | 356 | return $this->last_result[$y]?$this->last_result[$y]:null; |
| | 357 | } |
| | 358 | // If the output is an associative array then return row as such.. |
| | 359 | elseif ( $output == ARRAY_A ) { |
| | 360 | return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null; |
| | 361 | } |
| | 362 | // If the output is an numerical array then return row as such.. |
| | 363 | elseif ( $output == ARRAY_N ) { |
| | 364 | return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null; |
| | 365 | } |
| | 366 | // If invalid output type was specified.. |
| | 367 | else { |
| | 368 | $this->print_error(" \$wpdb->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"); |
| | 369 | } |
| | 370 | |
| | 371 | } |
| | 372 | |
| | 373 | // ================================================================== |
| | 374 | // Function to get 1 column from the cached result set based in X index |
| | 375 | // se docs for usage and info |
| | 376 | |
| | 377 | function get_col($query=null,$x=0) { |
| | 378 | |
| | 379 | // If there is a query then perform it if not then use cached results.. |
| | 380 | if ( $query ) { |
| | 381 | $this->query($query); |
| | 382 | } |
| | 383 | |
| | 384 | // Extract the column values |
| | 385 | for ( $i=0; $i < count($this->last_result); $i++ ) { |
| | 386 | $new_array[$i] = $this->get_var(null,$x,$i); |
| | 387 | } |
| | 388 | |
| | 389 | return $new_array; |
| | 390 | } |
| | 391 | |
| | 392 | // ================================================================== |
| | 393 | // Return the the query as a result set - see docs for more details |
| | 394 | |
| | 395 | function get_results($query=null, $output = OBJECT) { |
| | 396 | |
| | 397 | // Log how the function was called |
| | 398 | $this->func_call = "\$dwpb->get_results(\"$query\", $output)"; |
| | 399 | |
| | 400 | // If there is a query then perform it if not then use cached results.. |
| | 401 | if ( $query ) { |
| | 402 | $this->query($query); |
| | 403 | } |
| | 404 | |
| | 405 | // Send back array of objects. Each row is an object |
| | 406 | if ( $output == OBJECT ) { |
| | 407 | return $this->last_result; |
| | 408 | } |
| | 409 | elseif ( $output == ARRAY_A || $output == ARRAY_N ) { |
| | 410 | if ( $this->last_result ) { |
| | 411 | $i=0; |
| | 412 | foreach( $this->last_result as $row ) { |
| | 413 | |
| | 414 | $new_array[$i] = get_object_vars($row); |
| | 415 | |
| | 416 | if ( $output == ARRAY_N ) { |
| | 417 | $new_array[$i] = array_values($new_array[$i]); |
| | 418 | } |
| | 419 | |
| | 420 | $i++; |
| | 421 | } |
| | 422 | |
| | 423 | return $new_array; |
| | 424 | } else { |
| | 425 | return null; |
| | 426 | } |
| | 427 | } |
| | 428 | } |
| | 429 | |
| | 430 | // ================================================================== |
| | 431 | // Function to get column meta data info pertaining to the last query |
| | 432 | // see docs for more info and usage |
| | 433 | |
| | 434 | function get_col_info($info_type="name",$col_offset=-1) { |
| | 435 | |
| | 436 | if ( $this->col_info ) { |
| | 437 | if ( $col_offset == -1 ) { |
| | 438 | $i=0; |
| | 439 | foreach($this->col_info as $col ) { |
| | 440 | $new_array[$i] = $col->{$info_type}; |
| | 441 | $i++; |
| | 442 | } |
| | 443 | return $new_array; |
| | 444 | } else { |
| | 445 | return $this->col_info[$col_offset]->{$info_type}; |
| | 446 | } |
| | 447 | |
| | 448 | } |
| | 449 | |
| | 450 | } |
| | 451 | |
| | 452 | function timer_start() { |
| | 453 | $mtime = microtime(); |
| | 454 | $mtime = explode(' ', $mtime); |
| | 455 | $this->time_start = $mtime[1] + $mtime[0]; |
| | 456 | return true; |
| | 457 | } |
| | 458 | |
| | 459 | function timer_stop($precision = 3) { |
| | 460 | $mtime = microtime(); |
| | 461 | $mtime = explode(' ', $mtime); |
| | 462 | $time_end = $mtime[1] + $mtime[0]; |
| | 463 | $time_total = $time_end - $this->time_start; |
| | 464 | return $time_total; |
| | 465 | } |
| | 466 | |
| | 467 | // ================================================================== |
| | 468 | // Dumps the contents of any input variable to screen in a nicely |
| | 469 | // formatted and easy to understand way - any type: Object, Var or Array |
| | 470 | |
| | 471 | function vardump($mixed='') { |
| | 472 | |
| | 473 | echo "<p><table><tr><td bgcolor=ffffff><blockquote><font color=000090>"; |
| | 474 | echo "<pre><font face=arial>"; |
| | 475 | |
| | 476 | if ( ! $this->vardump_called ) { |
| | 477 | echo "<font color=800080><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Variable Dump..</b></font>\n\n"; |
| | 478 | } |
| | 479 | |
| | 480 | $var_type = gettype ($mixed); |
| | 481 | print_r(($mixed?$mixed:"<font color=red>No Value / False</font>")); |
| | 482 | echo "\n\n<b>Type:</b> " . ucfirst($var_type) . "\n"; |
| | 483 | echo "<b>Last Query</b> [$this->num_queries]<b>:</b> ".($this->last_query?$this->last_query:"NULL")."\n"; |
| | 484 | echo "<b>Last Function Call:</b> " . ($this->func_call?$this->func_call:"None")."\n"; |
| | 485 | echo "<b>Last Rows Returned:</b> ".count($this->last_result)."\n"; |
| | 486 | echo "</font></pre></font></blockquote></td></tr></table>".$this->donation(); |
| | 487 | echo "\n<hr size=1 noshade color=dddddd>"; |
| | 488 | |
| | 489 | $this->vardump_called = true; |
| | 490 | |
| | 491 | } |
| | 492 | |
| | 493 | // Alias for the above function |
| | 494 | function dumpvar($mixed) { |
| | 495 | $this->vardump($mixed); |
| | 496 | } |
| | 497 | |
| | 498 | // ================================================================== |
| | 499 | // Displays the last query string that was sent to the database & a |
| | 500 | // table listing results (if there were any). |
| | 501 | // (abstracted into a seperate file to save server overhead). |
| | 502 | |
| | 503 | function debug() { |
| | 504 | |
| | 505 | echo "<blockquote>"; |
| | 506 | |
| | 507 | // Only show ezSQL credits once.. |
| | 508 | if ( ! $this->debug_called ) { |
| | 509 | echo "<font color=800080 face=arial size=2><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Debug..</b></font><p>\n"; |
| | 510 | } |
| | 511 | echo "<font face=arial size=2 color=000099><b>Query</b> [$this->num_queries] <b>--</b> "; |
| | 512 | echo "[<font color=000000><b>$this->last_query</b></font>]</font><p>"; |
| | 513 | |
| | 514 | echo "<font face=arial size=2 color=000099><b>Query Result..</b></font>"; |
| | 515 | echo "<blockquote>"; |
| | 516 | |
| | 517 | if ( $this->col_info ) { |
| | 518 | |
| | 519 | // ===================================================== |
| | 520 | // Results top rows |
| | 521 | |
| | 522 | echo "<table cellpadding=5 cellspacing=1 bgcolor=555555>"; |
| | 523 | echo "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>"; |
| | 524 | |
| | 525 | |
| | 526 | for ( $i=0; $i < count($this->col_info); $i++ ) { |
| | 527 | echo "<td nowrap align=left valign=top><font size=1 color=555599 face=arial>{$this->col_info[$i]->type} {$this->col_info[$i]->max_length}</font><br><span style='font-family: arial; font-size: 10pt; font-weight: bold;'>{$this->col_info[$i]->name}</span></td>"; |
| | 528 | } |
| | 529 | |
| | 530 | echo "</tr>"; |
| | 531 | |
| | 532 | // ====================================================== |
| | 533 | // print main results |
| | 534 | |
| | 535 | if ( $this->last_result ) { |
| | 536 | |
| | 537 | $i=0; |
| | 538 | foreach ( $this->get_results(null,ARRAY_N) as $one_row ) { |
| | 539 | $i++; |
| | 540 | echo "<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>$i</font></td>"; |
| | 541 | |
| | 542 | foreach ( $one_row as $item ) { |
| | 543 | echo "<td nowrap><font face=arial size=2>$item</font></td>"; |
| | 544 | } |
| | 545 | |
| | 546 | echo "</tr>"; |
| | 547 | } |
| | 548 | |
| | 549 | } // if last result |
| | 550 | else { |
| | 551 | echo "<tr bgcolor=ffffff><td colspan=".(count($this->col_info)+1)."><font face=arial size=2>No Results</font></td></tr>"; |
| | 552 | } |
| | 553 | |
| | 554 | echo "</table>"; |
| | 555 | |
| | 556 | } // if col_info |
| | 557 | else { |
| | 558 | echo "<font face=arial size=2>No Results</font>"; |
| | 559 | } |
| | 560 | |
| | 561 | echo "</blockquote></blockquote>".$this->donation()."<hr noshade color=dddddd size=1>"; |
| | 562 | |
| | 563 | |
| | 564 | $this->debug_called = true; |
| | 565 | } |
| | 566 | |
| | 567 | |
| | 568 | } |
| | 569 | |
| | 570 | function record_tablename($stmt,$tabnam,$tail) { |
| | 571 | |
| | 572 | global $table_name; |
| | 573 | $table_name = $tabnam; |
| | 574 | return ($stmt."\"".$tabnam."\"".$tail); |
| | 575 | } |
| | 576 | |
| | 577 | function create_index($name,$content,$type=""){ |
| | 578 | |
| | 579 | global $table_name; |
| | 580 | global $poststmt; |
| | 581 | $poststmt.="CREATE $type INDEX \"".$table_name."_".$name."\" ON \"".$table_name."\"("; |
| | 582 | $cols=split(",",$content); |
| | 583 | $allvals=""; |
| | 584 | |
| | 585 | foreach ($cols as $vals) { |
| | 586 | $vals=preg_replace("/\(.*\)/","",$vals); |
| | 587 | $vals=preg_replace("/([a-zA-Z0-9_]+)/","\"$1\"",$vals); |
| | 588 | $allvals.=$vals; |
| | 589 | } |
| | 590 | |
| | 591 | $allvals=preg_replace("/\"\"/","\",\"",$allvals); |
| | 592 | $poststmt.=$allvals.");\n"; |
| | 593 | } |
| | 594 | |
| | 595 | function do_updates($tab,$content) { |
| | 596 | |
| | 597 | $ret="UPDATE \"".$tab."\" SET "; |
| | 598 | $cols=split(",",$content); |
| | 599 | |
| | 600 | foreach ($cols as $vals) { |
| | 601 | $vals=preg_replace("/([a-zA-Z0-9_]+)=([a-zA-Z0-9_]+)/","\"$1\"=\"$2\"",$vals); |
| | 602 | $ret.=$vals; |
| | 603 | } |
| | 604 | |
| | 605 | $ret=preg_replace("/\"\"/","\",\"",$ret); |
| | 606 | $ret=preg_replace("/`/","",$ret); |
| | 607 | |
| | 608 | return($ret); |
| | 609 | } |
| | 610 | |
| | 611 | function do_inserts($tab,$content,$tail) { |
| | 612 | |
| | 613 | // for some reason are the quotes in $tail addslashed. i dont know why |
| | 614 | $tail=preg_replace('/\\\"/','"',$tail); |
| | 615 | $ret="INSERT INTO \"".$tab."\" ("; |
| | 616 | $cols=split(",",$content); |
| | 617 | |
| | 618 | foreach ($cols as $vals) { |
| | 619 | $vals=preg_replace("/ /","",$vals); |
| | 620 | $ret.="\"$vals\""; |
| | 621 | } |
| | 622 | |
| | 623 | $ret=preg_replace("/\"\"/","\",\"",$ret); |
| | 624 | $ret.=")"; |
| | 625 | |
| | 626 | $tail=preg_replace("/md5\(\'(.+)\'\)/e","quotemd5('$1')",$tail); |
| | 627 | return $ret." VALUES ".$tail; |
| | 628 | } |
| | 629 | |
| | 630 | function quotemd5($a) { |
| | 631 | return ("'".md5($a)."'"); |
| | 632 | } |
| | 633 | |
| | 634 | function quote_prim_cols($key,$content) { |
| | 635 | |
| | 636 | $ret="\n $key ("; |
| | 637 | $cols=split(",",$content); |
| | 638 | |
| | 639 | foreach ($cols as $vals) { |
| | 640 | $vals=preg_replace("/\(.*\)/","",$vals); |
| | 641 | $ret.="\"".trim($vals)."\""; |
| | 642 | } |
| | 643 | |
| | 644 | $ret=preg_replace("/\"\"/","\",\"",$ret); |
| | 645 | $ret.=")"; |
| | 646 | |
| | 647 | return $ret; |
| | 648 | } |
| | 649 | |
| | 650 | function convert_enums($colname,$content) { |
| | 651 | |
| | 652 | $enumvals=split(",",$content); |
| | 653 | $isnum=true; |
| | 654 | $length=0; |
| | 655 | $colname=stripslashes($colname); |
| | 656 | $ret="\n $colname "; |
| | 657 | foreach ($enumvals as $vals) { |
| | 658 | if (!is_int($vals)) $isnum=false; |
| | 659 | if (strlen($vals)>$length) $length=strlen($vals); |
| | 660 | } |
| | 661 | if ($isnum) { |
| | 662 | if ($length < 4) $ret.="smallint "; |
| | 663 | elseif ($length < 9) $ret.="integer "; |
| | 664 | else $ret.="bigint "; |
| | 665 | } else { |
| | 666 | $ret.="varchar($length) "; |
| | 667 | } |
| | 668 | $ret.="CHECK ($colname IN ($content))"; |
| | 669 | |
| | 670 | return $ret; |
| | 671 | } |
| | 672 | |
| | 673 | $wpdb = new wpdb( DB_PATH, DB_NAME ); |
| | 674 | |
| | 675 | ?> |