"Fossies" - the Fresh Open Source Software Archive

Member "Upload/inc/db_mysql.php" (8 Jun 2019, 38051 Bytes) of package /linux/www/mybb_1821.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "db_mysql.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1820_vs_1821.

    1 <?php
    2 /**
    3  * MyBB 1.8
    4  * Copyright 2014 MyBB Group, All Rights Reserved
    5  *
    6  * Website: http://www.mybb.com
    7  * License: http://www.mybb.com/about/license
    8  *
    9  */
   10 
   11 class DB_MySQL implements DB_Base
   12 {
   13     /**
   14      * The title of this layer.
   15      *
   16      * @var string
   17      */
   18     public $title = "MySQL";
   19 
   20     /**
   21      * The short title of this layer.
   22      *
   23      * @var string
   24      */
   25     public $short_title = "MySQL";
   26 
   27     /**
   28      * The type of db software being used.
   29      *
   30      * @var string
   31      */
   32     public $type;
   33 
   34     /**
   35      * A count of the number of queries.
   36      *
   37      * @var int
   38      */
   39     public $query_count = 0;
   40 
   41     /**
   42      * A list of the performed queries.
   43      *
   44      * @var array
   45      */
   46     public $querylist = array();
   47 
   48     /**
   49      * 1 if error reporting enabled, 0 if disabled.
   50      *
   51      * @var boolean
   52      */
   53     public $error_reporting = 1;
   54 
   55     /**
   56      * The read database connection resource.
   57      *
   58      * @var resource
   59      */
   60     public $read_link;
   61 
   62     /**
   63      * The write database connection resource
   64      *
   65      * @var resource
   66      */
   67     public $write_link;
   68 
   69     /**
   70      * Reference to the last database connection resource used.
   71      *
   72      * @var resource
   73      */
   74     public $current_link;
   75 
   76     /**
   77      * The database name.
   78      *
   79      * @var string
   80      */
   81     public $database;
   82 
   83     /**
   84      * Explanation of a query.
   85      *
   86      * @var string
   87      */
   88     public $explain;
   89 
   90     /**
   91      * The current version of MySQL.
   92      *
   93      * @var string
   94      */
   95     public $version;
   96 
   97     /**
   98      * The current table type in use (myisam/innodb)
   99      *
  100      * @var string
  101      */
  102     public $table_type = "myisam";
  103 
  104     /**
  105      * The table prefix used for simple select, update, insert and delete queries
  106      *
  107      * @var string
  108      */
  109     public $table_prefix;
  110 
  111     /**
  112      * The extension used to run the SQL database
  113      *
  114      * @var string
  115      */
  116     public $engine = "mysql";
  117 
  118     /**
  119      * Weather or not this engine can use the search functionality
  120      *
  121      * @var boolean
  122      */
  123     public $can_search = true;
  124 
  125     /**
  126      * The database encoding currently in use (if supported)
  127      *
  128      * @var string
  129      */
  130     public $db_encoding = "utf8";
  131 
  132     /**
  133      * The time spent performing queries
  134      *
  135      * @var float
  136      */
  137     public $query_time = 0;
  138 
  139     /**
  140      * Stores previous run query type: 1 => write; 0 => read
  141      *
  142      * @var int
  143      */
  144 
  145     protected $last_query_type = 0;
  146 
  147     /**
  148      * Connect to the database server.
  149      *
  150      * @param array $config Array of DBMS connection details.
  151      * @return resource The DB connection resource. Returns false on fail or -1 on a db connect failure.
  152      */
  153     function connect($config)
  154     {
  155         // Simple connection to one server
  156         if(array_key_exists('hostname', $config))
  157         {
  158             $connections['read'][] = $config;
  159         }
  160         // Connecting to more than one server
  161         else
  162         {
  163             // Specified multiple servers, but no specific read/write servers
  164             if(!array_key_exists('read', $config))
  165             {
  166                 foreach($config as $key => $settings)
  167                 {
  168                     if(is_int($key))
  169                     {
  170                         $connections['read'][] = $settings;
  171                     }
  172                 }
  173             }
  174             // Specified both read & write servers
  175             else
  176             {
  177                 $connections = $config;
  178             }
  179         }
  180 
  181         $this->db_encoding = $config['encoding'];
  182 
  183         // Actually connect to the specified servers
  184         foreach(array('read', 'write') as $type)
  185         {
  186             if(!isset($connections[$type]) || !is_array($connections[$type]))
  187             {
  188                 break;
  189             }
  190 
  191             if(array_key_exists('hostname', $connections[$type]))
  192             {
  193                 $details = $connections[$type];
  194                 unset($connections);
  195                 $connections[$type][] = $details;
  196             }
  197 
  198             // Shuffle the connections
  199             shuffle($connections[$type]);
  200 
  201             // Loop-de-loop
  202             foreach($connections[$type] as $single_connection)
  203             {
  204                 $connect_function = "mysql_connect";
  205                 if(isset($single_connection['pconnect']))
  206                 {
  207                     $connect_function = "mysql_pconnect";
  208                 }
  209 
  210                 $link = "{$type}_link";
  211 
  212                 get_execution_time();
  213 
  214                 $this->$link = @$connect_function($single_connection['hostname'], $single_connection['username'], $single_connection['password'], 1);
  215 
  216                 $time_spent = get_execution_time();
  217                 $this->query_time += $time_spent;
  218 
  219                 // Successful connection? break down brother!
  220                 if($this->$link)
  221                 {
  222                     $this->connections[] = "[".strtoupper($type)."] {$single_connection['username']}@{$single_connection['hostname']} (Connected in ".format_time_duration($time_spent).")";
  223                     break;
  224                 }
  225                 else
  226                 {
  227                     $this->connections[] = "<span style=\"color: red\">[FAILED] [".strtoupper($type)."] {$single_connection['username']}@{$single_connection['hostname']}</span>";
  228                 }
  229             }
  230         }
  231 
  232         // No write server was specified (simple connection or just multiple servers) - mirror write link
  233         if(!array_key_exists('write', $connections))
  234         {
  235             $this->write_link = &$this->read_link;
  236         }
  237 
  238         // Have no read connection?
  239         if(!$this->read_link)
  240         {
  241             $this->error("[READ] Unable to connect to MySQL server");
  242             return false;
  243         }
  244         // No write?
  245         else if(!$this->write_link)
  246         {
  247             $this->error("[WRITE] Unable to connect to MySQL server");
  248             return false;
  249         }
  250 
  251         // Select databases
  252         if(!$this->select_db($config['database']))
  253         {
  254             return -1;
  255         }
  256 
  257         $this->current_link = &$this->read_link;
  258         return $this->read_link;
  259     }
  260 
  261     /**
  262      * Selects the database to use.
  263      *
  264      * @param string $database The database name.
  265      * @return boolean True when successfully connected, false if not.
  266      */
  267     function select_db($database)
  268     {
  269         $this->database = $database;
  270 
  271         $this->current_link = &$this->read_link;
  272         $read_success = @mysql_select_db($database, $this->read_link) or $this->error("[READ] Unable to select database", $this->read_link);
  273         if($this->write_link)
  274         {
  275             $this->current_link = &$this->write_link;
  276             $write_success = @mysql_select_db($database, $this->write_link) or $this->error("[WRITE] Unable to select database", $this->write_link);
  277             $success = ($read_success && $write_success ? true : false);
  278         }
  279         else
  280         {
  281             $success = $read_success;
  282         }
  283 
  284         if($success && $this->db_encoding)
  285         {
  286             // A little magic to support PHP 5.2.0, 5.2.1 and 5.2.2
  287             if(version_compare(PHP_VERSION, '5.2.3', '>=')) {
  288                 @mysql_set_charset($this->db_encoding, $this->read_link);
  289             }
  290             else
  291             {
  292                 $this->query("SET NAMES '{$this->db_encoding}'");
  293             }
  294 
  295             if($write_success && count($this->connections) > 1)
  296             {
  297                 if(version_compare(PHP_VERSION, '5.2.3', '>=')) {
  298                     @mysql_set_charset($this->db_encoding, $this->write_link);
  299                 }
  300                 else
  301                 {
  302                     $this->write_query("SET NAMES '{$this->db_encoding}'");
  303                 }
  304             }
  305         }
  306         return $success;
  307     }
  308 
  309     /**
  310      * Query the database.
  311      *
  312      * @param string $string The query SQL.
  313      * @param integer $hide_errors 1 if hide errors, 0 if not.
  314      * @param integer $write_query 1 if executes on master database, 0 if not.
  315      * @return resource The query data.
  316      */
  317     function query($string, $hide_errors=0, $write_query=0)
  318     {
  319         global $mybb;
  320 
  321         get_execution_time();
  322 
  323         // Only execute write queries on master database
  324         if(($write_query || $this->last_query_type) && $this->write_link)
  325         {
  326             $this->current_link = &$this->write_link;
  327             $query = @mysql_query($string, $this->write_link);
  328         }
  329         else
  330         {
  331             $this->current_link = &$this->read_link;
  332             $query = @mysql_query($string, $this->read_link);
  333         }
  334 
  335         if($this->error_number() && !$hide_errors)
  336         {
  337              $this->error($string);
  338              exit;
  339         }
  340 
  341         if($write_query)
  342         {
  343             $this->last_query_type = 1;
  344         }
  345         else
  346         {
  347             $this->last_query_type = 0;
  348         }
  349 
  350         $query_time = get_execution_time();
  351         $this->query_time += $query_time;
  352         $this->query_count++;
  353 
  354         if($mybb->debug_mode)
  355         {
  356             $this->explain_query($string, $query_time);
  357         }
  358 
  359         return $query;
  360     }
  361 
  362     /**
  363      * Execute a write query on the master database
  364      *
  365      * @param string $query The query SQL.
  366      * @param boolean|int $hide_errors 1 if hide errors, 0 if not.
  367      * @return resource The query data.
  368      */
  369     function write_query($query, $hide_errors=0)
  370     {
  371         return $this->query($query, $hide_errors, 1);
  372     }
  373 
  374     /**
  375      * Explain a query on the database.
  376      *
  377      * @param string $string The query SQL.
  378      * @param string $qtime The time it took to perform the query.
  379      */
  380     function explain_query($string, $qtime)
  381     {
  382         global $plugins;
  383 
  384         $debug_extra = '';
  385         if($plugins->current_hook)
  386         {
  387             $debug_extra = "<div style=\"float_right\">(Plugin Hook: {$plugins->current_hook})</div>";
  388         }
  389         if(preg_match("#^\s*select#i", $string))
  390         {
  391             $query = mysql_query("EXPLAIN $string", $this->current_link);
  392             $this->explain .= "<table style=\"background-color: #666;\" width=\"95%\" cellpadding=\"4\" cellspacing=\"1\" align=\"center\">\n".
  393                 "<tr>\n".
  394                 "<td colspan=\"8\" style=\"background-color: #ccc;\">{$debug_extra}<div><strong>#".$this->query_count." - Select Query</strong></div></td>\n".
  395                 "</tr>\n".
  396                 "<tr>\n".
  397                 "<td colspan=\"8\" style=\"background-color: #fefefe;\"><span style=\"font-family: Courier; font-size: 14px;\">".htmlspecialchars_uni($string)."</span></td>\n".
  398                 "</tr>\n".
  399                 "<tr style=\"background-color: #efefef;\">\n".
  400                 "<td><strong>Table</strong></td>\n".
  401                 "<td><strong>Type</strong></td>\n".
  402                 "<td><strong>Possible Keys</strong></td>\n".
  403                 "<td><strong>Key</strong></td>\n".
  404                 "<td><strong>Key Length</strong></td>\n".
  405                 "<td><strong>Ref</strong></td>\n".
  406                 "<td><strong>Rows</strong></td>\n".
  407                 "<td><strong>Extra</strong></td>\n".
  408                 "</tr>\n";
  409 
  410             while($table = mysql_fetch_array($query))
  411             {
  412                 $this->explain .=
  413                     "<tr bgcolor=\"#ffffff\">\n".
  414                     "<td>".$table['table']."</td>\n".
  415                     "<td>".$table['type']."</td>\n".
  416                     "<td>".$table['possible_keys']."</td>\n".
  417                     "<td>".$table['key']."</td>\n".
  418                     "<td>".$table['key_len']."</td>\n".
  419                     "<td>".$table['ref']."</td>\n".
  420                     "<td>".$table['rows']."</td>\n".
  421                     "<td>".$table['Extra']."</td>\n".
  422                     "</tr>\n";
  423             }
  424             $this->explain .=
  425                 "<tr>\n".
  426                 "<td colspan=\"8\" style=\"background-color: #fff;\">Query Time: ".format_time_duration($qtime)."</td>\n".
  427                 "</tr>\n".
  428                 "</table>\n".
  429                 "<br />\n";
  430         }
  431         else
  432         {
  433             $this->explain .= "<table style=\"background-color: #666;\" width=\"95%\" cellpadding=\"4\" cellspacing=\"1\" align=\"center\">\n".
  434                 "<tr>\n".
  435                 "<td style=\"background-color: #ccc;\">{$debug_extra}<div><strong>#".$this->query_count." - Write Query</strong></div></td>\n".
  436                 "</tr>\n".
  437                 "<tr style=\"background-color: #fefefe;\">\n".
  438                 "<td><span style=\"font-family: Courier; font-size: 14px;\">".htmlspecialchars_uni($string)."</span></td>\n".
  439                 "</tr>\n".
  440                 "<tr>\n".
  441                 "<td bgcolor=\"#ffffff\">Query Time: ".format_time_duration($qtime)."</td>\n".
  442                 "</tr>\n".
  443                 "</table>\n".
  444                 "<br />\n";
  445         }
  446 
  447         $this->querylist[$this->query_count]['query'] = $string;
  448         $this->querylist[$this->query_count]['time'] = $qtime;
  449     }
  450 
  451     /**
  452      * Return a result array for a query.
  453      *
  454      * @param resource $query The query ID.
  455      * @param int $resulttype The type of array to return. Either MYSQL_NUM, MYSQL_BOTH or MYSQL_ASSOC
  456      * @return array The array of results.
  457      */
  458     function fetch_array($query, $resulttype=MYSQL_ASSOC)
  459     {
  460         switch($resulttype)
  461         {
  462             case MYSQL_NUM:
  463             case MYSQL_BOTH:
  464                 break;
  465             default:
  466                 $resulttype = MYSQL_ASSOC;
  467                 break;
  468         }
  469 
  470         $array = mysql_fetch_array($query, $resulttype);
  471 
  472         return $array;
  473     }
  474 
  475     /**
  476      * Return a specific field from a query.
  477      *
  478      * @param resource $query The query ID.
  479      * @param string $field The name of the field to return.
  480      * @param int|bool $row The number of the row to fetch it from.
  481      * @return mixed
  482      */
  483     function fetch_field($query, $field, $row=false)
  484     {
  485         if($row === false)
  486         {
  487             $array = $this->fetch_array($query);
  488             return $array[$field];
  489         }
  490         else
  491         {
  492             return mysql_result($query, $row, $field);
  493         }
  494     }
  495 
  496     /**
  497      * Moves internal row pointer to the next row
  498      *
  499      * @param resource $query The query ID.
  500      * @param int $row The pointer to move the row to.
  501      * @return bool
  502      */
  503     function data_seek($query, $row)
  504     {
  505         return mysql_data_seek($query, $row);
  506     }
  507 
  508     /**
  509      * Return the number of rows resulting from a query.
  510      *
  511      * @param resource $query The query ID.
  512      * @return int The number of rows in the result.
  513      */
  514     function num_rows($query)
  515     {
  516         return mysql_num_rows($query);
  517     }
  518 
  519     /**
  520      * Return the last id number of inserted data.
  521      *
  522      * @return int The id number.
  523      */
  524     function insert_id()
  525     {
  526         return mysql_insert_id($this->current_link);
  527     }
  528 
  529     /**
  530      * Close the connection with the DBMS.
  531      *
  532      */
  533     function close()
  534     {
  535         @mysql_close($this->read_link);
  536         if($this->write_link)
  537         {
  538             @mysql_close($this->write_link);
  539         }
  540     }
  541 
  542     /**
  543      * Return an error number.
  544      *
  545      * @return int The error number of the current error.
  546      */
  547     function error_number()
  548     {
  549         if($this->current_link)
  550         {
  551             return @mysql_errno($this->current_link);
  552         }
  553         else
  554         {
  555             return @mysql_errno();
  556         }
  557     }
  558 
  559     /**
  560      * Return an error string.
  561      *
  562      * @return string The explanation for the current error.
  563      */
  564     function error_string()
  565     {
  566         if($this->current_link)
  567         {
  568             return @mysql_error($this->current_link);
  569         }
  570         else
  571         {
  572             return @mysql_error();
  573         }
  574     }
  575 
  576     /**
  577      * Output a database error.
  578      *
  579      * @param string $string The string to present as an error.
  580      * @return bool Returns false if error reporting is disabled, otherwise true
  581      */
  582     function error($string="")
  583     {
  584         if($this->error_reporting)
  585         {
  586             if(class_exists("errorHandler"))
  587             {
  588                 global $error_handler;
  589 
  590                 if(!is_object($error_handler))
  591                 {
  592                     require_once MYBB_ROOT."inc/class_error.php";
  593                     $error_handler = new errorHandler();
  594                 }
  595 
  596                 $error = array(
  597                     "error_no" => $this->error_number(),
  598                     "error" => $this->error_string(),
  599                     "query" => $string
  600                 );
  601                 $error_handler->error(MYBB_SQL, $error);
  602             }
  603             else
  604             {
  605                 trigger_error("<strong>[SQL] [".$this->error_number()."] ".$this->error_string()."</strong><br />{$string}", E_USER_ERROR);
  606             }
  607 
  608             return true;
  609         }
  610         else
  611         {
  612             return false;
  613         }
  614     }
  615 
  616     /**
  617      * Returns the number of affected rows in a query.
  618      *
  619      * @return int The number of affected rows.
  620      */
  621     function affected_rows()
  622     {
  623         return mysql_affected_rows($this->current_link);
  624     }
  625 
  626     /**
  627      * Return the number of fields.
  628      *
  629      * @param resource $query The query ID.
  630      * @return int The number of fields.
  631      */
  632     function num_fields($query)
  633     {
  634         return mysql_num_fields($query);
  635     }
  636 
  637     /**
  638      * Lists all tables in the database.
  639      *
  640      * @param string $database The database name.
  641      * @param string $prefix Prefix of the table (optional)
  642      * @return array The table list.
  643      */
  644     function list_tables($database, $prefix='')
  645     {
  646         if($prefix)
  647         {
  648             if(version_compare($this->get_version(), '5.0.2', '>='))
  649             {
  650                 $query = $this->query("SHOW FULL TABLES FROM `$database` WHERE table_type = 'BASE TABLE' AND `Tables_in_$database` LIKE '".$this->escape_string($prefix)."%'");
  651             }
  652             else
  653             {
  654                 $query = $this->query("SHOW TABLES FROM `$database` LIKE '".$this->escape_string($prefix)."%'");
  655             }
  656         }
  657         else
  658         {
  659             if(version_compare($this->get_version(), '5.0.2', '>='))
  660             {
  661                 $query = $this->query("SHOW FULL TABLES FROM `$database` WHERE table_type = 'BASE TABLE'");
  662             }
  663             else
  664             {
  665                 $query = $this->query("SHOW TABLES FROM `$database`");
  666             }
  667         }
  668 
  669         $tables = array();
  670         while(list($table) = mysql_fetch_array($query))
  671         {
  672             $tables[] = $table;
  673         }
  674 
  675         return $tables;
  676     }
  677 
  678     /**
  679      * Check if a table exists in a database.
  680      *
  681      * @param string $table The table name.
  682      * @return boolean True when exists, false if not.
  683      */
  684     function table_exists($table)
  685     {
  686         // Execute on master server to ensure if we've just created a table that we get the correct result
  687         if(version_compare($this->get_version(), '5.0.2', '>='))
  688         {
  689             $query = $this->query("SHOW FULL TABLES FROM `".$this->database."` WHERE table_type = 'BASE TABLE' AND `Tables_in_".$this->database."` = '{$this->table_prefix}$table'");
  690         }
  691         else
  692         {
  693             $query = $this->query("SHOW TABLES LIKE '{$this->table_prefix}$table'");
  694         }
  695 
  696         $exists = $this->num_rows($query);
  697         if($exists > 0)
  698         {
  699             return true;
  700         }
  701         else
  702         {
  703             return false;
  704         }
  705     }
  706 
  707     /**
  708      * Check if a field exists in a database.
  709      *
  710      * @param string $field The field name.
  711      * @param string $table The table name.
  712      * @return boolean True when exists, false if not.
  713      */
  714     function field_exists($field, $table)
  715     {
  716         $query = $this->write_query("
  717             SHOW COLUMNS
  718             FROM {$this->table_prefix}$table
  719             LIKE '$field'
  720         ");
  721         $exists = $this->num_rows($query);
  722         if($exists > 0)
  723         {
  724             return true;
  725         }
  726         else
  727         {
  728             return false;
  729         }
  730     }
  731 
  732     /**
  733      * Add a shutdown query.
  734      *
  735      * @param resource $query The query data.
  736      * @param string $name An optional name for the query.
  737      */
  738     function shutdown_query($query, $name="")
  739     {
  740         global $shutdown_queries;
  741         if($name)
  742         {
  743             $shutdown_queries[$name] = $query;
  744         }
  745         else
  746         {
  747             $shutdown_queries[] = $query;
  748         }
  749     }
  750     /**
  751      * Performs a simple select query.
  752      *
  753      * @param string $table The table name to be queried.
  754      * @param string $fields Comma delimetered list of fields to be selected.
  755      * @param string $conditions SQL formatted list of conditions to be matched.
  756      * @param array $options List of options: group by, order by, order direction, limit, limit start.
  757      * @return resource The query data.
  758      */
  759     function simple_select($table, $fields="*", $conditions="", $options=array())
  760     {
  761         $query = "SELECT ".$fields." FROM {$this->table_prefix}{$table}";
  762         if($conditions != "")
  763         {
  764             $query .= " WHERE ".$conditions;
  765         }
  766 
  767         if(isset($options['group_by']))
  768         {
  769             $query .= " GROUP BY ".$options['group_by'];
  770         }
  771 
  772         if(isset($options['order_by']))
  773         {
  774             $query .= " ORDER BY ".$options['order_by'];
  775             if(isset($options['order_dir']))
  776             {
  777                 $query .= " ".my_strtoupper($options['order_dir']);
  778             }
  779         }
  780 
  781         if(isset($options['limit_start']) && isset($options['limit']))
  782         {
  783             $query .= " LIMIT ".$options['limit_start'].", ".$options['limit'];
  784         }
  785         elseif(isset($options['limit']))
  786         {
  787             $query .= " LIMIT ".$options['limit'];
  788         }
  789 
  790         return $this->query($query);
  791     }
  792 
  793     /**
  794      * Build an insert query from an array.
  795      *
  796      * @param string $table The table name to perform the query on.
  797      * @param array $array An array of fields and their values.
  798      * @return int The insert ID if available
  799      */
  800     function insert_query($table, $array)
  801     {
  802         global $mybb;
  803 
  804         if(!is_array($array))
  805         {
  806             return false;
  807         }
  808 
  809         foreach($array as $field => $value)
  810         {
  811             if(isset($mybb->binary_fields[$table][$field]) && $mybb->binary_fields[$table][$field])
  812             {
  813                 if($value[0] != 'X') // Not escaped?
  814                 {
  815                     $value = $this->escape_binary($value);
  816                 }
  817                 
  818                 $array[$field] = $value;
  819             }
  820             else
  821             {
  822                 $array[$field] = $this->quote_val($value);
  823             }
  824         }
  825 
  826         $fields = "`".implode("`,`", array_keys($array))."`";
  827         $values = implode(",", $array);
  828         $this->write_query("
  829             INSERT
  830             INTO {$this->table_prefix}{$table} (".$fields.")
  831             VALUES (".$values.")
  832         ");
  833         return $this->insert_id();
  834     }
  835 
  836     /**
  837      * Build one query for multiple inserts from a multidimensional array.
  838      *
  839      * @param string $table The table name to perform the query on.
  840      * @param array $array An array of inserts.
  841      * @return void
  842      */
  843     function insert_query_multiple($table, $array)
  844     {
  845         global $mybb;
  846 
  847         if(!is_array($array))
  848         {
  849             return;
  850         }
  851 
  852         // Field names
  853         $fields = array_keys($array[0]);
  854         $fields = "`".implode("`,`", $fields)."`";
  855 
  856         $insert_rows = array();
  857         foreach($array as $values)
  858         {
  859             foreach($values as $field => $value)
  860             {
  861                 if(isset($mybb->binary_fields[$table][$field]) && $mybb->binary_fields[$table][$field])
  862                 {
  863                     if($value[0] != 'X') // Not escaped?
  864                     {
  865                         $value = $this->escape_binary($value);
  866                     }
  867                 
  868                     $values[$field] = $value;
  869                 }
  870                 else
  871                 {
  872                     $values[$field] = $this->quote_val($value);
  873                 }
  874             }
  875             $insert_rows[] = "(".implode(",", $values).")";
  876         }
  877         $insert_rows = implode(", ", $insert_rows);
  878 
  879         $this->write_query("
  880             INSERT
  881             INTO {$this->table_prefix}{$table} ({$fields})
  882             VALUES {$insert_rows}
  883         ");
  884     }
  885 
  886     /**
  887      * Build an update query from an array.
  888      *
  889      * @param string $table The table name to perform the query on.
  890      * @param array $array An array of fields and their values.
  891      * @param string $where An optional where clause for the query.
  892      * @param string $limit An optional limit clause for the query.
  893      * @param boolean $no_quote An option to quote incoming values of the array.
  894      * @return resource The query data.
  895      */
  896     function update_query($table, $array, $where="", $limit="", $no_quote=false)
  897     {
  898         global $mybb;
  899 
  900         if(!is_array($array))
  901         {
  902             return false;
  903         }
  904 
  905         $comma = "";
  906         $query = "";
  907         $quote = "'";
  908 
  909         if($no_quote == true)
  910         {
  911             $quote = "";
  912         }
  913 
  914         foreach($array as $field => $value)
  915         {
  916             if(isset($mybb->binary_fields[$table][$field]) && $mybb->binary_fields[$table][$field])
  917             {
  918                 if($value[0] != 'X') // Not escaped?
  919                 {
  920                     $value = $this->escape_binary($value);
  921                 }
  922                 
  923                 $query .= $comma."`".$field."`={$value}";
  924             }
  925             else
  926             {
  927                 $quoted_val = $this->quote_val($value, $quote);
  928 
  929                 $query .= $comma."`".$field."`={$quoted_val}";
  930             }
  931             $comma = ', ';
  932         }
  933 
  934         if(!empty($where))
  935         {
  936             $query .= " WHERE $where";
  937         }
  938 
  939         if(!empty($limit))
  940         {
  941             $query .= " LIMIT $limit";
  942         }
  943 
  944         return $this->write_query("
  945             UPDATE {$this->table_prefix}$table
  946             SET $query
  947         ");
  948     }
  949 
  950     /**
  951      * @param int|string $value
  952      * @param string $quote
  953      *
  954      * @return int|string
  955      */
  956     private function quote_val($value, $quote="'")
  957     {
  958         if(is_int($value))
  959         {
  960             $quoted = $value;
  961         }
  962         else
  963         {
  964             $quoted = $quote . $value . $quote;
  965         }
  966 
  967         return $quoted;
  968     }
  969 
  970     /**
  971      * Build a delete query.
  972      *
  973      * @param string $table The table name to perform the query on.
  974      * @param string $where An optional where clause for the query.
  975      * @param string $limit An optional limit clause for the query.
  976      * @return resource The query data.
  977      */
  978     function delete_query($table, $where="", $limit="")
  979     {
  980         $query = "";
  981         if(!empty($where))
  982         {
  983             $query .= " WHERE $where";
  984         }
  985 
  986         if(!empty($limit))
  987         {
  988             $query .= " LIMIT $limit";
  989         }
  990 
  991         return $this->write_query("
  992             DELETE
  993             FROM {$this->table_prefix}$table
  994             $query
  995         ");
  996     }
  997 
  998     /**
  999      * Escape a string according to the MySQL escape format.
 1000      *
 1001      * @param string $string The string to be escaped.
 1002      * @return string The escaped string.
 1003      */
 1004     function escape_string($string)
 1005     {
 1006         if($this->db_encoding == 'utf8')
 1007         {
 1008             $string = validate_utf8_string($string, false);
 1009         }
 1010         elseif($this->db_encoding == 'utf8mb4')
 1011         {
 1012             $string = validate_utf8_string($string);
 1013         }
 1014 
 1015         if(function_exists("mysql_real_escape_string") && $this->read_link)
 1016         {
 1017             $string = mysql_real_escape_string($string, $this->read_link);
 1018         }
 1019         else
 1020         {
 1021             $string = addslashes($string);
 1022         }
 1023         return $string;
 1024     }
 1025 
 1026     /**
 1027      * Frees the resources of a MySQLi query.
 1028      *
 1029      * @param resource $query The query to destroy.
 1030      * @return boolean Returns true on success, false on faliure
 1031      */
 1032     function free_result($query)
 1033     {
 1034         return mysql_free_result($query);
 1035     }
 1036 
 1037     /**
 1038      * Escape a string used within a like command.
 1039      *
 1040      * @param string $string The string to be escaped.
 1041      * @return string The escaped string.
 1042      */
 1043     function escape_string_like($string)
 1044     {
 1045         return $this->escape_string(str_replace(array('%', '_') , array('\\%' , '\\_') , $string));
 1046     }
 1047 
 1048     /**
 1049      * Gets the current version of MySQL.
 1050      *
 1051      * @return string Version of MySQL.
 1052      */
 1053     function get_version()
 1054     {
 1055         if($this->version)
 1056         {
 1057             return $this->version;
 1058         }
 1059 
 1060         $query = $this->query("SELECT VERSION() as version");
 1061         $ver = $this->fetch_array($query);
 1062         $version = $ver['version'];
 1063 
 1064         if($version)
 1065         {
 1066             $version = explode(".", $version, 3);
 1067             $this->version = (int)$version[0].".".(int)$version[1].".".(int)$version[2];
 1068         }
 1069         return $this->version;
 1070     }
 1071 
 1072     /**
 1073      * Optimizes a specific table.
 1074      *
 1075      * @param string $table The name of the table to be optimized.
 1076      */
 1077     function optimize_table($table)
 1078     {
 1079         $this->write_query("OPTIMIZE TABLE {$this->table_prefix}{$table}");
 1080     }
 1081 
 1082     /**
 1083      * Analyzes a specific table.
 1084      *
 1085      * @param string $table The name of the table to be analyzed.
 1086      */
 1087     function analyze_table($table)
 1088     {
 1089         $this->write_query("ANALYZE TABLE {$this->table_prefix}{$table}");
 1090     }
 1091 
 1092     /**
 1093      * Show the "create table" command for a specific table.
 1094      *
 1095      * @param string $table The name of the table.
 1096      * @return string The MySQL command to create the specified table.
 1097      */
 1098     function show_create_table($table)
 1099     {
 1100         $query = $this->write_query("SHOW CREATE TABLE {$this->table_prefix}{$table}");
 1101         $structure = $this->fetch_array($query);
 1102         return $structure['Create Table'];
 1103     }
 1104 
 1105     /**
 1106      * Show the "show fields from" command for a specific table.
 1107      *
 1108      * @param string $table The name of the table.
 1109      * @return array Field info for that table
 1110      */
 1111     function show_fields_from($table)
 1112     {
 1113         $query = $this->write_query("SHOW FIELDS FROM {$this->table_prefix}{$table}");
 1114         $field_info = array();
 1115         while($field = $this->fetch_array($query))
 1116         {
 1117             $field_info[] = $field;
 1118         }
 1119         return $field_info;
 1120     }
 1121 
 1122     /**
 1123      * Returns whether or not the table contains a fulltext index.
 1124      *
 1125      * @param string $table The name of the table.
 1126      * @param string $index Optionally specify the name of the index.
 1127      * @return boolean True or false if the table has a fulltext index or not.
 1128      */
 1129     function is_fulltext($table, $index="")
 1130     {
 1131         $structure = $this->show_create_table($table);
 1132         if($index != "")
 1133         {
 1134             if(preg_match("#FULLTEXT KEY (`?)$index(`?)#i", $structure))
 1135             {
 1136                 return true;
 1137             }
 1138             else
 1139             {
 1140                 return false;
 1141             }
 1142         }
 1143         if(preg_match('#FULLTEXT KEY#i', $structure))
 1144         {
 1145             return true;
 1146         }
 1147         return false;
 1148     }
 1149 
 1150     /**
 1151      * Returns whether or not this database engine supports fulltext indexing.
 1152      *
 1153      * @param string $table The table to be checked.
 1154      * @return boolean True or false if supported or not.
 1155      */
 1156 
 1157     function supports_fulltext($table)
 1158     {
 1159         $version = $this->get_version();
 1160         $query = $this->write_query("SHOW TABLE STATUS LIKE '{$this->table_prefix}$table'");
 1161         $status = $this->fetch_array($query);
 1162         if($status['Engine'])
 1163         {
 1164             $table_type = my_strtoupper($status['Engine']);
 1165         }
 1166         else
 1167         {
 1168             $table_type = my_strtoupper($status['Type']);
 1169         }
 1170         if(version_compare($version, '3.23.23', '>=') && ($table_type == 'MYISAM' || $table_type == 'ARIA'))
 1171         {
 1172             return true;
 1173         }
 1174         elseif(version_compare($version, '5.6', '>=') && $table_type == 'INNODB')
 1175         {
 1176             return true;
 1177         }
 1178         return false;
 1179     }
 1180 
 1181     /**
 1182      * Checks to see if an index exists on a specified table
 1183      *
 1184      * @param string $table The name of the table.
 1185      * @param string $index The name of the index.
 1186      * @return bool Whether or not the index exists in that table
 1187      */
 1188     function index_exists($table, $index)
 1189     {
 1190         $index_exists = false;
 1191         $query = $this->write_query("SHOW INDEX FROM {$this->table_prefix}{$table}");
 1192         while($ukey = $this->fetch_array($query))
 1193         {
 1194             if($ukey['Key_name'] == $index)
 1195             {
 1196                 $index_exists = true;
 1197                 break;
 1198             }
 1199         }
 1200 
 1201         if($index_exists)
 1202         {
 1203             return true;
 1204         }
 1205 
 1206         return false;
 1207     }
 1208 
 1209     /**
 1210      * Returns whether or not this database engine supports boolean fulltext matching.
 1211      *
 1212      * @param string $table The table to be checked.
 1213      * @return boolean True or false if supported or not.
 1214      */
 1215     function supports_fulltext_boolean($table)
 1216     {
 1217         $version = $this->get_version();
 1218         $supports_fulltext = $this->supports_fulltext($table);
 1219         if(version_compare($version, '4.0.1', '>=') && $supports_fulltext == true)
 1220         {
 1221             return true;
 1222         }
 1223         return false;
 1224     }
 1225 
 1226     /**
 1227      * Creates a fulltext index on the specified column in the specified table with optional index name.
 1228      *
 1229      * @param string $table The name of the table.
 1230      * @param string $column Name of the column to be indexed.
 1231      * @param string $name The index name, optional.
 1232      */
 1233     function create_fulltext_index($table, $column, $name="")
 1234     {
 1235         $this->write_query("
 1236             ALTER TABLE {$this->table_prefix}$table
 1237             ADD FULLTEXT $name ($column)
 1238         ");
 1239     }
 1240 
 1241     /**
 1242      * Drop an index with the specified name from the specified table
 1243      *
 1244      * @param string $table The name of the table.
 1245      * @param string $name The name of the index.
 1246      */
 1247     function drop_index($table, $name)
 1248     {
 1249         $this->write_query("
 1250             ALTER TABLE {$this->table_prefix}$table
 1251             DROP INDEX $name
 1252         ");
 1253     }
 1254 
 1255     /**
 1256      * Drop an table with the specified table
 1257      *
 1258      * @param string $table The table to drop
 1259      * @param boolean $hard hard drop - no checking
 1260      * @param boolean $table_prefix use table prefix
 1261      */
 1262     function drop_table($table, $hard=false, $table_prefix=true)
 1263     {
 1264         if($table_prefix == false)
 1265         {
 1266             $table_prefix = "";
 1267         }
 1268         else
 1269         {
 1270             $table_prefix = $this->table_prefix;
 1271         }
 1272 
 1273         if($hard == false)
 1274         {
 1275             $this->write_query('DROP TABLE IF EXISTS '.$table_prefix.$table);
 1276         }
 1277         else
 1278         {
 1279             $this->write_query('DROP TABLE '.$table_prefix.$table);
 1280         }
 1281     }
 1282 
 1283     /**
 1284      * Renames a table
 1285      *
 1286      * @param string $old_table The old table name
 1287      * @param string $new_table the new table name
 1288      * @param boolean $table_prefix use table prefix
 1289      * @return resource
 1290      */
 1291     function rename_table($old_table, $new_table, $table_prefix=true)
 1292     {
 1293         if($table_prefix == false)
 1294         {
 1295             $table_prefix = "";
 1296         }
 1297         else
 1298         {
 1299             $table_prefix = $this->table_prefix;
 1300         }
 1301 
 1302         return $this->write_query("RENAME TABLE {$table_prefix}{$old_table} TO {$table_prefix}{$new_table}");
 1303     }
 1304 
 1305     /**
 1306      * Replace contents of table with values
 1307      *
 1308      * @param string $table The table
 1309      * @param array $replacements The replacements
 1310      * @param string|array $default_field The default field(s)
 1311      * @param boolean $insert_id Whether or not to return an insert id. True by default
 1312      * @return resource|bool
 1313      */
 1314     function replace_query($table, $replacements=array(), $default_field="", $insert_id=true)
 1315     {
 1316         global $mybb;
 1317 
 1318         $values = '';
 1319         $comma = '';
 1320         foreach($replacements as $column => $value)
 1321         {
 1322             if(isset($mybb->binary_fields[$table][$column]) && $mybb->binary_fields[$table][$column])
 1323             {
 1324                 if($value[0] != 'X') // Not escaped?
 1325                 {
 1326                     $value = $this->escape_binary($value);
 1327                 }
 1328                 
 1329                 $values .= $comma."`".$column."`=".$value;
 1330             }
 1331             else
 1332             {
 1333                 $values .= $comma."`".$column."`=".$this->quote_val($value);
 1334             }
 1335 
 1336             $comma = ',';
 1337         }
 1338 
 1339         if(empty($replacements))
 1340         {
 1341              return false;
 1342         }
 1343 
 1344         return $this->write_query("REPLACE INTO {$this->table_prefix}{$table} SET {$values}");
 1345     }
 1346 
 1347     /**
 1348      * Drops a column
 1349      *
 1350      * @param string $table The table
 1351      * @param string $column The column name
 1352      * @return resource
 1353      */
 1354     function drop_column($table, $column)
 1355     {
 1356         $column = trim($column, '`');
 1357 
 1358         return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} DROP `{$column}`");
 1359     }
 1360 
 1361     /**
 1362      * Adds a column
 1363      *
 1364      * @param string $table The table
 1365      * @param string $column The column name
 1366      * @param string $definition the new column definition
 1367      * @return resource
 1368      */
 1369     function add_column($table, $column, $definition)
 1370     {
 1371         $column = trim($column, '`');
 1372 
 1373         return $this->write_query("ALTER TABLE {$this->table_prefix}{$table} ADD `{$column}` {$definition}");
 1374     }
 1375 
 1376     /**
 1377      * Modifies a column
 1378      *
 1379      * @param string $table The table
 1380      * @param string $column The column name
 1381      * @param string $new_definition the new column definition
 1382      * @param boolean|string $new_not_null Whether to "drop" or "set" the NOT NULL attribute (no change if false)
 1383      * @param boolean|string $new_default_value The new default value, or false to drop the attribute
 1384      * @return bool Returns true if all queries are executed successfully or false if one of them failed
 1385      */
 1386     function modify_column($table, $column, $new_definition, $new_not_null=false, $new_default_value=false)
 1387     {
 1388         $column = trim($column, '`');
 1389 
 1390         if($new_not_null !== false)
 1391         {
 1392             if(strtolower($new_not_null) == "set")
 1393             {
 1394                 $not_null = "NOT NULL";
 1395             }
 1396             else
 1397             {
 1398                 $not_null = "NULL";
 1399             }
 1400         }
 1401         else
 1402         {
 1403             $not_null = '';
 1404         }
 1405 
 1406         if($new_default_value !== false)
 1407         {
 1408             $default = "DEFAULT ".$new_default_value;
 1409         }
 1410         else
 1411         {
 1412             $default = '';
 1413         }
 1414 
 1415         return (bool)$this->write_query("ALTER TABLE {$this->table_prefix}{$table} MODIFY `{$column}` {$new_definition} {$not_null}");
 1416     }
 1417 
 1418     /**
 1419      * Renames a column
 1420      *
 1421      * @param string $table The table
 1422      * @param string $old_column The old column name
 1423      * @param string $new_column the new column name
 1424      * @param string $new_definition the new column definition
 1425      * @param boolean|string $new_not_null Whether to "drop" or "set" the NOT NULL attribute (no change if false)
 1426      * @param boolean|string $new_default_value The new default value, or false to drop the attribute
 1427      * @return bool Returns true if all queries are executed successfully
 1428      */
 1429     function rename_column($table, $old_column, $new_column, $new_definition, $new_not_null=false, $new_default_value=false)
 1430     {
 1431         $old_column = trim($old_column, '`');
 1432         $new_column = trim($new_column, '`');
 1433 
 1434         if($new_not_null !== false)
 1435         {
 1436             if(strtolower($new_not_null) == "set")
 1437             {
 1438                 $not_null = "NOT NULL";
 1439             }
 1440             else
 1441             {
 1442                 $not_null = "NULL";
 1443             }
 1444         }
 1445         else
 1446         {
 1447             $not_null = '';
 1448         }
 1449 
 1450         if($new_default_value !== false)
 1451         {
 1452             $default = "DEFAULT ".$new_default_value;
 1453         }
 1454         else
 1455         {
 1456             $default = '';
 1457         }
 1458 
 1459         return (bool)$this->write_query("ALTER TABLE {$this->table_prefix}{$table} CHANGE `{$old_column}` `{$new_column}` {$new_definition} {$not_null} {$default}");
 1460     }
 1461 
 1462     /**
 1463      * Sets the table prefix used by the simple select, insert, update and delete functions
 1464      *
 1465      * @param string $prefix The new table prefix
 1466      */
 1467     function set_table_prefix($prefix)
 1468     {
 1469         $this->table_prefix = $prefix;
 1470     }
 1471 
 1472     /**
 1473      * Fetched the total size of all mysql tables or a specific table
 1474      *
 1475      * @param string $table The table (optional)
 1476      * @return integer the total size of all mysql tables or a specific table
 1477      */
 1478     function fetch_size($table='')
 1479     {
 1480         if($table != '')
 1481         {
 1482             $query = $this->query("SHOW TABLE STATUS LIKE '".$this->table_prefix.$table."'");
 1483         }
 1484         else
 1485         {
 1486             $query = $this->query("SHOW TABLE STATUS");
 1487         }
 1488         $total = 0;
 1489         while($table = $this->fetch_array($query))
 1490         {
 1491             $total += $table['Data_length']+$table['Index_length'];
 1492         }
 1493         return $total;
 1494     }
 1495 
 1496     /**
 1497      * Fetch a list of database character sets this DBMS supports
 1498      *
 1499      * @return array|bool Array of supported character sets with array key being the name, array value being display name. False if unsupported
 1500      */
 1501     function fetch_db_charsets()
 1502     {
 1503         if($this->write_link && version_compare($this->get_version(), "4.1", "<"))
 1504         {
 1505             return false;
 1506         }
 1507         return array(
 1508             'big5' => 'Big5 Traditional Chinese',
 1509             'dec8' => 'DEC West European',
 1510             'cp850' => 'DOS West European',
 1511             'hp8' => 'HP West European',
 1512             'koi8r' => 'KOI8-R Relcom Russian',
 1513             'latin1' => 'ISO 8859-1 Latin 1',
 1514             'latin2' => 'ISO 8859-2 Central European',
 1515             'swe7' => '7bit Swedish',
 1516             'ascii' => 'US ASCII',
 1517             'ujis' => 'EUC-JP Japanese',
 1518             'sjis' => 'Shift-JIS Japanese',
 1519             'hebrew' => 'ISO 8859-8 Hebrew',
 1520             'tis620' => 'TIS620 Thai',
 1521             'euckr' => 'EUC-KR Korean',
 1522             'koi8u' => 'KOI8-U Ukrainian',
 1523             'gb2312' => 'GB2312 Simplified Chinese',
 1524             'greek' => 'ISO 8859-7 Greek',
 1525             'cp1250' => 'Windows Central European',
 1526             'gbk' => 'GBK Simplified Chinese',
 1527             'latin5' => 'ISO 8859-9 Turkish',
 1528             'armscii8' => 'ARMSCII-8 Armenian',
 1529             'utf8' => 'UTF-8 Unicode',
 1530             'utf8mb4' => '4-Byte UTF-8 Unicode (requires MySQL 5.5.3 or above)',
 1531             'ucs2' => 'UCS-2 Unicode',
 1532             'cp866' => 'DOS Russian',
 1533             'keybcs2' => 'DOS Kamenicky Czech-Slovak',
 1534             'macce' => 'Mac Central European',
 1535             'macroman' => 'Mac West European',
 1536             'cp852' => 'DOS Central European',
 1537             'latin7' => 'ISO 8859-13 Baltic',
 1538             'cp1251' => 'Windows Cyrillic',
 1539             'cp1256' => 'Windows Arabic',
 1540             'cp1257' => 'Windows Baltic',
 1541             'geostd8' => 'GEOSTD8 Georgian',
 1542             'cp932' => 'SJIS for Windows Japanese',
 1543             'eucjpms' => 'UJIS for Windows Japanese',
 1544         );
 1545     }
 1546 
 1547     /**
 1548      * Fetch a database collation for a particular database character set
 1549      *
 1550      * @param string  $charset The database character set
 1551      * @return string|bool The matching database collation, false if unsupported
 1552      */
 1553     function fetch_charset_collation($charset)
 1554     {
 1555         $collations = array(
 1556             'big5' => 'big5_chinese_ci',
 1557             'dec8' => 'dec8_swedish_ci',
 1558             'cp850' => 'cp850_general_ci',
 1559             'hp8' => 'hp8_english_ci',
 1560             'koi8r' => 'koi8r_general_ci',
 1561             'latin1' => 'latin1_swedish_ci',
 1562             'latin2' => 'latin2_general_ci',
 1563             'swe7' => 'swe7_swedish_ci',
 1564             'ascii' => 'ascii_general_ci',
 1565             'ujis' => 'ujis_japanese_ci',
 1566             'sjis' => 'sjis_japanese_ci',
 1567             'hebrew' => 'hebrew_general_ci',
 1568             'tis620' => 'tis620_thai_ci',
 1569             'euckr' => 'euckr_korean_ci',
 1570             'koi8u' => 'koi8u_general_ci',
 1571             'gb2312' => 'gb2312_chinese_ci',
 1572             'greek' => 'greek_general_ci',
 1573             'cp1250' => 'cp1250_general_ci',
 1574             'gbk' => 'gbk_chinese_ci',
 1575             'latin5' => 'latin5_turkish_ci',
 1576             'armscii8' => 'armscii8_general_ci',
 1577             'utf8' => 'utf8_general_ci',
 1578             'utf8mb4' => 'utf8mb4_general_ci',
 1579             'ucs2' => 'ucs2_general_ci',
 1580             'cp866' => 'cp866_general_ci',
 1581             'keybcs2' => 'keybcs2_general_ci',
 1582             'macce' => 'macce_general_ci',
 1583             'macroman' => 'macroman_general_ci',
 1584             'cp852' => 'cp852_general_ci',
 1585             'latin7' => 'latin7_general_ci',
 1586             'cp1251' => 'cp1251_general_ci',
 1587             'cp1256' => 'cp1256_general_ci',
 1588             'cp1257' => 'cp1257_general_ci',
 1589             'geostd8' => 'geostd8_general_ci',
 1590             'cp932' => 'cp932_japanese_ci',
 1591             'eucjpms' => 'eucjpms_japanese_ci',
 1592         );
 1593         if($collations[$charset])
 1594         {
 1595             return $collations[$charset];
 1596         }
 1597         return false;
 1598     }
 1599 
 1600     /**
 1601      * Fetch a character set/collation string for use with CREATE TABLE statements. Uses current DB encoding
 1602      *
 1603      * @return string The built string, empty if unsupported
 1604      */
 1605     function build_create_table_collation()
 1606     {
 1607         if(!$this->db_encoding)
 1608         {
 1609             return '';
 1610         }
 1611 
 1612         $collation = $this->fetch_charset_collation($this->db_encoding);
 1613         if(!$collation)
 1614         {
 1615             return '';
 1616         }
 1617         return " CHARACTER SET {$this->db_encoding} COLLATE {$collation}";
 1618     }
 1619 
 1620     /**
 1621      * Time how long it takes for a particular piece of code to run. Place calls above & below the block of code.
 1622      *
 1623      * @deprecated
 1624      */
 1625     function get_execution_time()
 1626     {
 1627         return get_execution_time();
 1628     }
 1629 
 1630     /**
 1631      * Binary database fields require special attention.
 1632      *
 1633      * @param string $string Binary value
 1634      * @return string Encoded binary value
 1635      */
 1636     function escape_binary($string)
 1637     {
 1638         return "X'".$this->escape_string(bin2hex($string))."'";
 1639     }
 1640 
 1641     /**
 1642      * Unescape binary data.
 1643      *
 1644      * @param string $string Binary value
 1645      * @return string Encoded binary value
 1646      */
 1647     function unescape_binary($string)
 1648     {
 1649         // Nothing to do
 1650         return $string;
 1651     }
 1652 }
 1653