Script Last Updated: Friday, August 21st, 2009
Documentation Last Updated: Thursday, August 20th, 2009
Demo Last Updated: Saturday, August 15th, 2009
The Advanced MySQL Database Class is probably one of the simplest database objects/classes to use for PHP. Once the object is created, the number of lines of code that you need to use for any function in this class is just 1! Any query at all can be called and return a result whether it be data or true/false. Other things this class can do is: retrieve database information, backup one or all of your databases to either a SQL string variable or a SQL file, execute queries from a SQL file, print a results table, sort a non-indexed table's records, and prevent SQL injection.
If you want to just copy and paste the code instead of downloading it, use this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 |
<?php // ============================================================================= // // Script: class.database.php // // Version 2.4.0 // // Author: Matt DeKok (C) 2009 // // Contact: http://www.animestreamers.com/contact/ // // Documentation: http://www.animestreamers.com/portfolio/class_database/ // // Demo: http://www.animestreamers.com/portfolio/class_database_demo/ // // ============================================================================= // // Description: // // The Advanced MySQL Database Class is probably one of the simplest database // // objects/classes to use for PHP. The number of lines of code that you need to // // use for any function in this class is just 1! Any query at all can be called // // and return a result whether it be data or true/false. Other things this class // // can do is: retrieve database information, backup one or all of your databases // // to either a SQL string variable or a SQL file, execute queries from a SQL // // file, print a results table, sort a non-indexed table's records, and prevent // // SQL injection. // // ============================================================================= // // Updates Log: // // // // Version 1.0 - Years ago // // - Old class that was not very user friendly // // // // Version 2.0.0 - 07/27/2009 // // - New user-friendly class that is more functional than version 1.0. // // - Unlike the previous version, this class comes with a single query function // // capable of handling all queries. // // - It includes several new functions such as: // // - get_table_fields // // - get_tables // // - get_databases // // - get_mysql_variables // // - write_backup_sql // // // // Version 2.1.0 - 07/31/2009 // // - Updates log added // // - New function: sort_nonindexed_table (For more information, read below.) // // // // Version 2.1.1 - 08/01/2009 // // - __construct() improved by not requiring a database selection. Not all // // queries require one. // // - New function: select_db // // The purpose of this function is to allow a database selection if one is not // // selected during the construction of the database object. Also it will allow // // the user to switch between databases while using just one object. // // - write_backup_sql() function was improved by putting quotes around numbers // // that contain non-numeric characters. It also puts a NULL value in fields // // that are primary keys and auto incremented. // // // // Version 2.1.2 - 08/03/2009 // // - New function: disconnect (For more information, read below.) // // // // Version 2.2.0 - 08/04/2009 // // - You can now return just one record with multiple fields to a 1-dimensional // // array using database::GET_ROW in the query function's parameters. // // - New function: execute_file (For more information, read below.) // // - New function: sql_date_format (For more information, read below.) // // - Improved error handling // // // // Version 2.2.1 - 08/05/2009 // // - Fixed a bug in the write_backup_sql function by changing how it handles // // generating code for CREATE TABLE commands. // // // // Version 2.3.0 - 08/09/2009 // // - New function: print_table (For more information, read below.) // // // // Version 2.4.0 - 08/20/2009 // // - New function: prevent_injection // // Basically if you use this function on the values being updated or inserted // // in your database it will prevent injected SQL from causing any harm to your // // database. NOTE: Do not use quotes around your non-numeric values if you use // // this function, it will add them for you. // // // // ============================================================================= // class database { public $affected_rows = 0; // Use after calling query function public $insert_id = null; // Use after calling an insert query public $connection; // For use with mysql php functions public $result; // For use with mysql php functions private $crlf = "\r\n"; private $as_file = false; private $tmp_buffer = ""; private $db_host = ""; private $db_user = ""; private $db_pass = ""; private $db_name = ""; const GET_ARRAY = 0; const GET_ROW = 1; const GET_FIELD = 2; const VTYPE_BOTH = "BOTH"; const VTYPE_SESSION = "SESSION"; const VTYPE_GLOBAL = "GLOBAL"; function __construct($db_host, $db_user, $db_pass, $db_name = '') { $this->db_host = $db_host; $this->db_user = $db_user; $this->db_pass = $db_pass; $this->db_name = $db_name; //connect to database $connection = @mysql_connect($db_host, $db_user, $db_pass); if (!$connection) { $this->show_error('Could not connect to database server.'); } $this->connection = $connection; //select database if($db_name != '') { $db = @mysql_select_db($db_name, $connection); if (!$db) { $this->show_error('Could not connect to database.'); } } } // ----------------------------------------------- // select_db($db_name) // ----------------------------------------------- // Description: Allows you to switch databases // // Parameters: // $db_name = Name of the database to connect to // ----------------------------------------------- public function select_db($db_name) { $db = @mysql_select_db($db_name, $this->connection); if(!$db) { $this->show_error('Could not connect to database.'); return false; } return true; } // ----------------------------------------------- // disconnect() // ----------------------------------------------- // Description: Disconnects the connection // // Parameters: none // ----------------------------------------------- public function disconnect() { return mysql_close($this->connection); } // ----------------------------------------------- // query($query, $return_type = self::GET_ARRAY) // ----------------------------------------------- // Description: Runs a MySQL query // // Parameters: // $query = MySQL query // $return_type = database::GET_ARRAY (default) // Returns results to a 2-dimensional array // database::GET_ROW // Returns result record to an array // database::GET_FIELD // Returns result to a variable // // Notes: // 1. After a query is called if it affected any rows, then you can use // the included $db->affected_rows variable to get the number of // rows the query affected. ($db is just an example) // 2. After calling an INSERT query, you can use $db->insert_id to get // the value of the ID generated. // 3. Will store the connection and result resource variables in public // variables that may be used outside of this class with mysql_ // functions. // ----------------------------------------------- public function query($query, $return_type = self::GET_ARRAY) { // Determine query type $q = trim($query); $qarr = explode(' ', $q); // Proceed based on query type if (strtoupper($qarr[0]) == "SELECT" || strtoupper($qarr[0]) == "SHOW" || strtoupper($qarr[0]) == "EXPLAIN" || strtoupper($qarr[0]) == "DESCRIBE" || strtoupper($qarr[0]) == "HELP") { // Send query $this->result = @mysql_query($query); if (!$this->result) { $this->show_error(mysql_error()); @mysql_free_result($this->result); return false; } else { // Output number of affected rows $this->affected_rows = mysql_affected_rows(); // Get array of field names $fields = array(); $num_fields = mysql_num_fields($this->result); for($i=0;$i<$num_fields;$i++) { $fields[] = mysql_field_name($this->result, $i); } // Run through data returned $n = 0; $data = array(); while ($row=mysql_fetch_array($this->result, MYSQL_ASSOC)) { if($return_type == self::GET_ROW) return $row; foreach($fields as $f) { $field = trim($f); if ($return_type == self::GET_FIELD) return $row[$field]; $data[$n] = $row; } $n++; } } // Free result @mysql_free_result($this->result); // Show error return $data; } else { // Send query $this->result = @mysql_query($query); if (!$this->result) { $this->show_error(mysql_error()); return false; } else { // Output number of affected rows $this->affected_rows = mysql_affected_rows(); } // If the query was an INSERT query, then store the insert_id if(strtoupper($qarr[0]) == "INSERT") $this->insert_id = mysql_insert_id(); // Free result @mysql_free_result($this->result); // Return result return true; } } // ----------------------------------------------- // get_table_fields($table_name, $exclude = array()) // ----------------------------------------------- // Description: Returns an array of fields in the chosen table // // Parameters: // $table_name = Name of a table to retrieve the fields from // $exclude = Array of fields to exclude from results (i.e. id fields) // // Notes: // 1. It will return information about each field including Field, Type, // Null, Key, Default, and Extra // ----------------------------------------------- public function get_table_fields($table_name, $exclude = array()) { $structure = $this->query("SHOW COLUMNS FROM `$table_name`;"); if (is_array($structure)) { $fields = array(); foreach ($structure as $k => $v) { if (!in_array($v["Field"], $exclude)) { $fields[] = $v; } } return $fields; } return false; } // ----------------------------------------------- // get_tables($exclude = array()) // ----------------------------------------------- // Description: Returns an array of tables in the database // // Parameters: // $exclude = Array of tables to exclude from results // ----------------------------------------------- public function get_tables($exclude = array()) { $structure = $this->query("SHOW TABLES FROM `".$this->db_name."`;"); if (is_array($structure)) { $tables = array(); foreach ($structure as $k => $v) { if (!in_array($v["Tables_in_".$this->db_name], $exclude)) { $tables[] = $v["Tables_in_".$this->db_name]; } } return $tables; } return false; } // ----------------------------------------------- // get_databases($exclude = array()) // ----------------------------------------------- // Description: Returns an array of databases excluding system databases // // Parameters: // $exclude = Array of databases to exclude from results // ----------------------------------------------- public function get_databases($exclude = array()) { $exc = array('information_schema','mysql'); $exclude_all = array_merge($exclude, $exc); $structure = $this->query("SHOW DATABASES;"); if (is_array($structure)) { $databases = array(); foreach ($structure as $k => $v) { if (!in_array($v["Database"], $exclude_all)) { $databases[] = $v["Database"]; } } return $databases; } return false; } // ----------------------------------------------- // get_mysql_variables() // ----------------------------------------------- // Description: Returns an array of the MySQL variables and their values // // Parameters: // $variable_type = They type of variable // database::VTYPE_BOTH (default) // database::VTYPE_SESSION // database::VTYPE_GLOBAL // ----------------------------------------------- public function get_mysql_variables($variable_type = self::VTYPE_BOTH) { return $this->query('SHOW ' . ($variable_type != self::BOTH ? $variable_type : '') . ' VARIABLES;'); } // ----------------------------------------------- // sort_nonindexed_table($table_name, $columns = array()) // ----------------------------------------------- // Description: Sorts a table with no index column by the chosen column. // // // Parameters: // $table_name = Name of table being sorted // $columns = Array of columns/fields being sorted by // Example syntaxes: // As an array: array('`column_1` ASC','`column_2` DESC') // As a string: '`column_1` ASC, `column_2` ASC' // // Returns: // Success = true // Failure = false // // Notes: // The table will not stay sorted after INSERTs and UPDATEs. // ----------------------------------------------- public function sort_nonindexed_table($table_name, $columns = array()) { if(is_array($columns)) $c = implode(", ",$columns); else $c = $columns; return $this->query("ALTER TABLE `$table_name` ORDER BY $c;"); } // ----------------------------------------------- // write_backup_sql($generate_file = false, $all_dbs = false) // ----------------------------------------------- // Description: Write MySQL backup code for your database // // Parameters: // $generate_file = User can opt to have this generate an SQL file. // If true, then this function must be called before // anything is output to browser when headers are called. // $all_dbs = User may also opt to have this function backup all the // databases on their host. (Better when $generate_file = true) // // Returns: // If $generate_file = true, then result is an SQL file. // If $generate_file = false, then result is a string. // ----------------------------------------------- public function write_backup_sql($generate_file = false, $all_dbs = true) { // If the user wants to generate an SQL file, then convert the Content Type if ($generate_file) { $this->as_file = $generate_file; header('Content-Type: application/octetstream'); header('Content-Disposition: filename="' . $this->db_name . '.sql"'); } // Write header $dump_buffer = "# MySQL Database Dump".$this->crlf; $dump_buffer .= "# Backup made: " . date("F j, Y, g:i a") . $this->crlf; $dump_buffer .= "# Database: " . ($all_dbs == true ? 'All Databases' : $this->db_name) . $this->crlf; if(!$all_dbs) $dump_buffer .= "# Backed up tables: " . count($this->get_tables()) . $this->crlf; else $dump_buffer .= "# Backed up databases: " . count($this->get_databases()) . $this->crlf; $dump_buffer .= "# Generated by: class_database by Matt DeKok" .$this->crlf; if($all_dbs) { // Retrieve SQL dump for each database $db_name = $this->db_name; $dbs = $this->get_databases(); foreach($dbs as $db) { $this->db_name = $db; $dump_buffer .= $this->generate_sql($generate_file, $db); } $this->db_name = $db_name; } else { // Retrieve SQL dump for each table $dump_buffer .= $this->generate_sql($generate_file, $this->db_name); } // Output SQL dump if ($generate_file) { echo $dump_buffer; exit; } else { return $dump_buffer; } } // ----------------------------------------------- // execute_file($file) // ----------------------------------------------- // Description: Execute the MySQL commands inside a file // // Parameters: // $file = The path of the file // // Returns: // On success = true // On failure = false // ----------------------------------------------- public function execute_file($file) { if(!file_exists($file)) { $this->show_error("File '$file' does not exist."); return false; } $content = file_get_contents($file); if(!$content) { $this->show_error("Unable to read '$file'."); return false; } $sql = explode(";",$content); foreach($sql as $command) { if(!empty($command)) { if(!$this->query($command)) return false; } } return true; } // ----------------------------------------------- // sql_date_format($date) // ----------------------------------------------- // Description: Converts a timestamp or string format date // to MySQL format so it can be inserted into // a table // // Parameters: // $date = A numeric timestamp or a string date // ----------------------------------------------- public function sql_date_format($date) { if(gettype($date) == 'string') $value = strtotime($date); return date('Y-m-d H:i:s', $value); } // ----------------------------------------------- // print_table($results, // $row_color = null, // $alternate_row_color = null, // $row_style = null, // $table_style = null, // $header_row_style = null) // ----------------------------------------------- // Description: Prints a table from a query. // // Parameters: // $results = The associative array returned from the query function // $row_color = The primary row color // $alternate_row_color = The alternate row color // $row_style = The style of every row // $table_style = The table style // $header_row_style = The header row style // ----------------------------------------------- public function print_table($results, $row_color = null, $alternate_row_color = null, $row_style = null, $table_style = null, $header_row_style = null) { if(!is_array($results)) return false; if($row_color == null) $row_color = "#fff"; if($alternate_row_color == null) $alternate_row_color = "#ddd"; if($row_style == null) $row_style = "vertical-align: top;"; if($table_style == null) $table_style = "border: 2px solid #888; border-collapse: collapse;"; if($header_row_style == null) $header_row_style = "text-align: left; background-color: #ccc; border-bottom: 2px solid #888; color: #000;"; $headers = array_keys($results[0]); echo "<div style=\"overflow: auto; overflow-y: hidden;\">\n"; echo "<table style=\"$table_style\">\n"; echo " <tr>\n"; foreach($headers as $header) { echo " <th style=\"$header_row_style\">$header</th>\n"; } echo " </tr>\n"; $i = 0; foreach($results as $result) { $i++; if(floor($i / 2) == $i / 2) $style_row = "background-color: $row_color; $row_style"; else $style_row = "background-color: $alternate_row_color; $row_style"; echo " <tr style=\"$style_row\">\n"; foreach($headers as $header) { $item = $result[$header]; echo " <td>$item</td>\n"; } echo " </tr>\n"; } echo "</table>\n"; echo "</div>\n"; } // ----------------------------------------------- // prevent_injection($value) // ----------------------------------------------- // Description: If you use this on all your values for UPDATE // and INSERT queries. // // Note: It will be better to let this add the quotes to your // strings for you. // ----------------------------------------------- public function prevent_injection($value) { // Stripslashes if (get_magic_quotes_gpc()) { $value = stripslashes($value); } // Quote if not a number or a numeric string if (!is_numeric($value)) { $value = "'" . mysql_real_escape_string($value) . "'"; } return $value; } // ----------------------------------------------- // Pay no mind to the following functions // ----------------------------------------------- private function show_error($error) { echo "<p><strong>Error:</strong> " . $error . "</p>"; } private function get_table_def($table) { $this->error = array(); $schema_create = "DROP TABLE IF EXISTS `" . $this->db_name . "`.`$table`;".$this->crlf; $db = $table; $structure = $this->query("SHOW CREATE TABLE `$table`;"); $schema_create = $structure[0]['Create Table']; $schema_create = str_replace("`$table`","`" . $this->db_name . "`.`$table`",$schema_create); if (get_magic_quotes_gpc()) { return (stripslashes($schema_create)); } else { return ($schema_create); } } private function get_table_content($table, $limit_from = 0, $limit_to = 0) { // Defines the offsets to use if ($limit_from > 0) { $limit_from--; } else { $limit_from = 0; } if ($limit_to > 0 && $limit_from >= 0) { $add_query = " LIMIT $limit_from, $limit_to"; } else { $add_query = ''; } $result = mysql_query('SELECT * FROM ' . $this->db_name . '.' . $table . $add_query); if (!$result) { $this->show_error(mysql_error()); return false; } else { @set_time_limit(1200); // 20 Minutes // Checks whether the field is an integer or not for ($j = 0; $j < mysql_num_fields($result); $j++) { $field_set[$j] = mysql_field_name($result, $j); $type[$j] = mysql_field_type($result, $j); $primary[$j] = substr_count(mysql_field_flags($result, $j),"primary_key"); $auto[$j] = substr_count(mysql_field_flags($result, $j),"auto_increment"); if ($type[$j] == 'tinyint' || $type[$j] == 'smallint' || $type[$j] == 'mediumint' || $type[$j] == 'int' || $type[$j] == 'bigint' || $type[$j] == 'timestamp') { $field_num[$j] = true; } else { $field_num[$j] = false; } } // end for // Get the scheme if (isset($GLOBALS['showcolumns'])) { $fields = implode('`, `', $field_set); $schema_insert = "INSERT INTO `" . $this->db_name . "`.`$table` (`$fields`) VALUES ("; } else { $schema_insert = "INSERT INTO `" . $this->db_name . "`.`$table` VALUES ("; } $field_count = mysql_num_fields($result); $search = array("\x0a", "\x0d", "\x1a", "'"); //\x08\\x09, not required $replace = array("\\n", "\\r", "\Z", "\\'"); while ($row = mysql_fetch_row($result)) { for ($j = 0; $j < $field_count; $j++) { if (!isset($row[$j]) || ($primary[$j] && $auto[$j])) { $values[] = 'NULL'; } elseif (!empty($row[$j])) { // a number if ($field_num[$j] && is_numeric($row[$j])) { $values[] = $row[$j]; } // a string else { $values[] = "'" . str_replace($search, $replace, $row[$j]) . "'"; } } else { $values[] = "''"; } // end if } // end for $insert_line = $schema_insert . implode(', ', $values) . ')'; unset($values); // Call the handler $this->handler($insert_line); } // end while } return true; } private function handler($sql_insert) { if (!$this->as_file) $this->tmp_buffer .= htmlspecialchars("$sql_insert;" . $this->crlf); else $this->tmp_buffer .= "$sql_insert;" . $this->crlf; } private function faqe_db_error() { return mysql_error(); } private function faqe_db_insert_id($result) { return mysql_insert_id($result); } private function generate_sql($generate_file, $db_name) { $dump_buffer = ""; $dump_buffer .= $this->crlf."# --------------------------------------------------------".$this->crlf.$this->crlf; $dump_buffer .= "#".$this->crlf; $dump_buffer .= "# Database structure for database '$db_name'".$this->crlf; $dump_buffer .= "#".$this->crlf; $dump_buffer .= "DROP DATABASE IF EXISTS `$db_name`;".$this->crlf; $cdb = $this->query("SHOW CREATE DATABASE `$db_name`;"); $dump_buffer .= str_replace(" /*!40100 DEFAULT CHARACTER SET latin1 */",";",$cdb[0]["Create Database"]).$this->crlf; $dump_buffer .= $this->crlf; $tables = $this->query("SHOW TABLES FROM `" . $db_name . "`;"); $num_tables = $this->affected_rows; // Check if tables were found if ($num_tables == 0) { if ($generate_file) { echo "# 0 tables found"; } else { return false; } } foreach($tables as $table) { $table = $table["Tables_in_$db_name"]; // Write table structure $dump_buffer .= "# --------------------------------------------------------".$this->crlf; $dump_buffer .= $this->crlf."#".$this->crlf; $dump_buffer .= "# Table structure for table '$table'".$this->crlf; $dump_buffer .= "#".$this->crlf; $dump_buffer .= $this->get_table_def($table) . ";".$this->crlf; // Write table data $dump_buffer .= $this->crlf."#".$this->crlf; $dump_buffer .= "# Dumping data for table '$table'".$this->crlf; $dump_buffer .= "#".$this->crlf; $this->tmp_buffer = ""; $this->get_table_content($table, 0, 0); $dump_buffer .= $this->tmp_buffer; $dump_buffer .= $this->crlf; } return $dump_buffer; } private function get_column_type($table_name, $column_name) { $result = $this->query("SELECT $column_name FROM $table_name;"); if(!$result) { return false; } $type = mysql_field_type($result, 0); if(!$type) { $this->show_error(mysql_error()); $return = false; } @mysql_free_result($result); return $type; } } ?> |
The first line of code that needs to be called before you can make a query is to call the class/object itself.
1 2 3 4 5 |
// Calling the object with a database selected $db = new database($db_host,$db_user,$db_pass,$db_name); // Calling the object without a database selected $db = new database($db_host,$db_user,$db_pass); |
The only other line of code you need is to call the query itself.
The reason why selecting a database isn't always necessary is that not all queries, such as the HELP query, require a database.
If you wish to open or switch to another database, you may use the following function:
1 2 |
// Starting/switching to a database $db->select_db($db_name); |
If the database is selected, then this function will return true. Otherwise, it will return false.
Then after completing all of your queries you can disconnect the connection by calling this:
1 |
$db->disconnect(); |
The queries that return results include:
If you want more information on these queries, please read the MySQL Reference Manual.
With class_database, these queries can be called 3 ways. The default method will return an array for when more than one record or field is being returned. This is the default method that can be used if you do not know how many records will be returned. The following snippet demonstrates how to call it and how to use it.
1 2 3 4 5 6 7 8 |
// Calling a query to a 2-dimensional array $arr = $db->query("SELECT name, phone FROM users;"); // Using the results for($i=0;$i<count($arr);$i++) { echo "Name: ".$arr[$i]["name"]."<br />"; echo "Phone: ".$arr[$i]["phone"]."<br /><br />"; } |
The next method is when you're calling just one record, but want the values from more than one field.
1 2 3 4 5 6 7 |
// Calling a query to an array $row = $db->query("SELECT name, phone FROM users WHERE user_id = 34;", database::GET_ROW); // Using the results foreach($row as $field=>$value) { echo $field." = ".$value."<br />"; } |
The last method to call the queries is useful for when you just want one value from the database and want it stored right into a variable.
1 2 3 4 5 |
// Calling a query to a variable $phone = $db->query("SELECT phone FROM users WHERE user_id = 34;", database::GET_FIELD); // Using the results echo "Phone: ".$phone; |
When you need to call any other kind of query such as INSERT or UPDATE, you do it the same way as those that return results,
but the value returned is true or false. Easy, huh?
There are 2 types of information that can be retrieved using this class: a array of table names for all tables in the database and an array of the field names of fields in a specified table.
1 2 3 4 5 6 7 8 9 10 11 12 |
// Getting the names of all databases on your host $databases = $db->get_databases(); // Getting the array of tables $tables = $db->get_tables(); // Getting the array of fields in the 'users' table // It will return information about each field including Field, Type, Null, Key, Default, and Extra $fields = $db->get_table_fields('users'); // Getting MySQL Variables $variables = $db->get_mysql_variables(); |
Plus, there is an optional field on each of these functions allow the user to exclude certain tables and fields by using an array.
Using class.database.php, you are able to backup your database or all of the databases on your host. You may also choose to have the function generate an SQL file or a string
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Backing up all databases // Generating SQL String echo $db->write_backup_sql(); // Generating SQL File $db->write_backup_sql(true); // Backing up only the selected database // Generating SQL String echo $db->write_backup_sql(false, false); // Generating SQL File $db->write_backup_sql(true, false); |
In addition to being able to backup your database, class.database.php can also import code from an SQL file.
1 2 3 4 5 |
// Importing the file if($db->execute_file('exported_database.sql') echo 'Import completed successfully!'; else echo 'Import failed.'; |
class.database is able to sort a table with no index by 1 or more columns. It is not a permanent sort, so whenever an
INSERT or UPDATE is called, the items in the table may no longer be sorted.
1 2 |
// Sorting the table $db->sort_nonindexed_table('nonindexed_table',array('column1 ASC','column2 ASC')); |
Okay, this is not a built in feature of this class, but I want to let you know of an easy way to do this using a MySQL
statement called LOAD DATA. The MySQL Reference
Manual has more information about this function, but here are the nuts and bolts of what you need to know to get your
data from CSV files.
1 2 3 4 5 |
LOAD DATA INFILE 'http://www.yoursite.com/file.csv' INTO TABLE t1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' (column1, @var1) SET column2 = @var1/100; |
1 2 3 4 5 |
LOAD DATA INFILE 'http://www.yoursite.com/file.csv' INTO TABLE t1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' (column1, column2) SET column3 = CURRENT_TIMESTAMP; |
SET statements to store those variables into columns of your table.
1 2 3 4 |
LOAD DATA INFILE 'http://www.yoursite.com/file.csv' INTO TABLE t1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' (column1, @dummy, column2, @dummy, column3); |
Also as you can see in the examples above, you can choose what delimiter your file uses and what type of quotation is used.
If an input line has too many fields, the extra fields are ignored and the number of warnings is incremented.
If an input line has too few fields, the table columns for which input fields are missing are set to their default values. Default value assignment is described in Section 10.1.4, “Data Type Default Values” of the MySQL Reference Manual.
An empty field value is interpreted differently than if the field value is missing:
class.database is able to help you prevent SQL injection from taking place in your queries when you use the prevent_injection
function. The following code demonstrates how to use it.
Important Note: Do not quote your non-numeric values that you use this on. It does it for you so doing so would result in double quotes.
1 2 3 4 5 6 7 8 9 10 |
// The following will result in double quotes because // '' were added around the %s in the query string. $query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'", mysql_real_escape_string($db->prevent_injection($_POST['user'])), mysql_real_escape_string($db->prevent_injection($_POST['password']))); // Do it like this $query = sprintf("SELECT * FROM users WHERE user=%s AND password=%s", mysql_real_escape_string($db->prevent_injection($_POST['user'])), mysql_real_escape_string($db->prevent_injection($_POST['password']))); |
802 Unique Visitors