home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-includes / wp-db.php < prev    next >
Encoding:
PHP Script  |  2008-07-11  |  23.8 KB  |  971 lines

  1. <?php
  2. /**
  3.  * WordPress DB Class
  4.  *
  5.  * Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)}
  6.  *
  7.  * @package WordPress
  8.  * @subpackage Database
  9.  * @since 0.71
  10.  */
  11.  
  12. /**
  13.  * @since 0.71
  14.  */
  15. define('EZSQL_VERSION', 'WP1.25');
  16.  
  17. /**
  18.  * @since 0.71
  19.  */
  20. define('OBJECT', 'OBJECT', true);
  21.  
  22. /**
  23.  * @since {@internal Version Unknown}}
  24.  */
  25. define('OBJECT_K', 'OBJECT_K', false);
  26.  
  27. /**
  28.  * @since 0.71
  29.  */
  30. define('ARRAY_A', 'ARRAY_A', false);
  31.  
  32. /**
  33.  * @since 0.71
  34.  */
  35. define('ARRAY_N', 'ARRAY_N', false);
  36.  
  37. /**
  38.  * WordPress Database Access Abstraction Object
  39.  *
  40.  * It is possible to replace this class with your own
  41.  * by setting the $wpdb global variable in wp-content/wpdb.php
  42.  * file with your class. You can name it wpdb also, since
  43.  * this file will not be included, if the other file is
  44.  * available.
  45.  *
  46.  * @link http://codex.wordpress.org/Function_Reference/wpdb_Class
  47.  *
  48.  * @package WordPress
  49.  * @subpackage Database
  50.  * @since 0.71
  51.  * @final
  52.  */
  53. class wpdb {
  54.  
  55.     /**
  56.      * Whether to show SQL/DB errors
  57.      *
  58.      * @since 0.71
  59.      * @access private
  60.      * @var bool
  61.      */
  62.     var $show_errors = false;
  63.  
  64.     /**
  65.      * Whether to suppress errors during the DB bootstrapping.
  66.      *
  67.      * @access private
  68.      * @since {@internal Version Unknown}}
  69.      * @var bool
  70.      */
  71.     var $suppress_errors = false;
  72.  
  73.     /**
  74.      * The last error during query.
  75.      *
  76.      * @since {@internal Version Unknown}}
  77.      * @var string
  78.      */
  79.     var $last_error = '';
  80.  
  81.     /**
  82.      * Amount of queries made
  83.      *
  84.      * @since 1.2.0
  85.      * @access private
  86.      * @var int
  87.      */
  88.     var $num_queries = 0;
  89.  
  90.     /**
  91.      * Saved result of the last query made
  92.      *
  93.      * @since 1.2.0
  94.      * @access private
  95.      * @var array
  96.      */
  97.     var $last_query;
  98.  
  99.     /**
  100.      * Saved info on the table column
  101.      *
  102.      * @since 1.2.0
  103.      * @access private
  104.      * @var array
  105.      */
  106.     var $col_info;
  107.  
  108.     /**
  109.      * Saved queries that were executed
  110.      *
  111.      * @since 1.5.0
  112.      * @access private
  113.      * @var array
  114.      */
  115.     var $queries;
  116.  
  117.     /**
  118.      * WordPress table prefix
  119.      *
  120.      * You can set this to have multiple WordPress installations
  121.      * in a single database. The second reason is for possible
  122.      * security precautions.
  123.      *
  124.      * @since 0.71
  125.      * @access private
  126.      * @var string
  127.      */
  128.     var $prefix = '';
  129.  
  130.     /**
  131.      * Whether the database queries are ready to start executing.
  132.      *
  133.      * @since 2.5.0
  134.      * @access private
  135.      * @var bool
  136.      */
  137.     var $ready = false;
  138.  
  139.     /**
  140.      * WordPress Posts table
  141.      *
  142.      * @since 1.5.0
  143.      * @access public
  144.      * @var string
  145.      */
  146.     var $posts;
  147.  
  148.     /**
  149.      * WordPress Users table
  150.      *
  151.      * @since 1.5.0
  152.      * @access public
  153.      * @var string
  154.      */
  155.     var $users;
  156.  
  157.     /**
  158.      * WordPress Categories table
  159.      *
  160.      * @since 1.5.0
  161.      * @access public
  162.      * @var string
  163.      */
  164.     var $categories;
  165.  
  166.     /**
  167.      * WordPress Post to Category table
  168.      *
  169.      * @since 1.5.0
  170.      * @access public
  171.      * @var string
  172.      */
  173.     var $post2cat;
  174.  
  175.     /**
  176.      * WordPress Comments table
  177.      *
  178.      * @since 1.5.0
  179.      * @access public
  180.      * @var string
  181.      */
  182.     var $comments;
  183.  
  184.     /**
  185.      * WordPress Links table
  186.      *
  187.      * @since 1.5.0
  188.      * @access public
  189.      * @var string
  190.      */
  191.     var $links;
  192.  
  193.     /**
  194.      * WordPress Options table
  195.      *
  196.      * @since 1.5.0
  197.      * @access public
  198.      * @var string
  199.      */
  200.     var $options;
  201.  
  202.     /**
  203.      * WordPress Post Metadata table
  204.      *
  205.      * @since {@internal Version Unknown}}
  206.      * @access public
  207.      * @var string
  208.      */
  209.     var $postmeta;
  210.  
  211.     /**
  212.      * WordPress User Metadata table
  213.      *
  214.      * @since 2.3.0
  215.      * @access public
  216.      * @var string
  217.      */
  218.     var $usermeta;
  219.  
  220.     /**
  221.      * WordPress Terms table
  222.      *
  223.      * @since 2.3.0
  224.      * @access public
  225.      * @var string
  226.      */
  227.     var $terms;
  228.  
  229.     /**
  230.      * WordPress Term Taxonomy table
  231.      *
  232.      * @since 2.3.0
  233.      * @access public
  234.      * @var string
  235.      */
  236.     var $term_taxonomy;
  237.  
  238.     /**
  239.      * WordPress Term Relationships table
  240.      *
  241.      * @since 2.3.0
  242.      * @access public
  243.      * @var string
  244.      */
  245.     var $term_relationships;
  246.  
  247.     /**
  248.      * List of WordPress tables
  249.      *
  250.      * @since {@internal Version Unknown}}
  251.      * @access private
  252.      * @var array
  253.      */
  254.     var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
  255.             'postmeta', 'terms', 'term_taxonomy', 'term_relationships');
  256.  
  257.     /**
  258.      * Database table columns charset
  259.      *
  260.      * @since 2.2.0
  261.      * @access public
  262.      * @var string
  263.      */
  264.     var $charset;
  265.  
  266.     /**
  267.      * Database table columns collate
  268.      *
  269.      * @since 2.2.0
  270.      * @access public
  271.      * @var string
  272.      */
  273.     var $collate;
  274.  
  275.     /**
  276.      * Connects to the database server and selects a database
  277.      *
  278.      * PHP4 compatibility layer for calling the PHP5 constructor.
  279.      *
  280.      * @uses wpdb::__construct() Passes parameters and returns result
  281.      * @since 0.71
  282.      *
  283.      * @param string $dbuser MySQL database user
  284.      * @param string $dbpassword MySQL database password
  285.      * @param string $dbname MySQL database name
  286.      * @param string $dbhost MySQL database host
  287.      */
  288.     function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
  289.         return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost);
  290.     }
  291.  
  292.     /**
  293.      * Connects to the database server and selects a database
  294.      *
  295.      * PHP5 style constructor for compatibility with PHP5. Does
  296.      * the actual setting up of the class properties and connection
  297.      * to the database.
  298.      *
  299.      * @since 2.0.8
  300.      *
  301.      * @param string $dbuser MySQL database user
  302.      * @param string $dbpassword MySQL database password
  303.      * @param string $dbname MySQL database name
  304.      * @param string $dbhost MySQL database host
  305.      */
  306.     function __construct($dbuser, $dbpassword, $dbname, $dbhost) {
  307.         register_shutdown_function(array(&$this, "__destruct"));
  308.  
  309.         if ( defined('WP_DEBUG') and WP_DEBUG == true )
  310.             $this->show_errors();
  311.  
  312.         if ( defined('DB_CHARSET') )
  313.             $this->charset = DB_CHARSET;
  314.  
  315.         if ( defined('DB_COLLATE') )
  316.             $this->collate = DB_COLLATE;
  317.  
  318.         $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true);
  319.         if (!$this->dbh) {
  320.             $this->bail(sprintf(/*WP_I18N_DB_CONN_ERROR*/"
  321. <h1>Error establishing a database connection</h1>
  322. <p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>%s</code>. This could mean your host's database server is down.</p>
  323. <ul>
  324.     <li>Are you sure you have the correct username and password?</li>
  325.     <li>Are you sure that you have typed the correct hostname?</li>
  326.     <li>Are you sure that the database server is running?</li>
  327. </ul>
  328. <p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
  329. "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost));
  330.             return;
  331.         }
  332.  
  333.         $this->ready = true;
  334.  
  335.         if ( $this->supports_collation() ) {
  336.             $collation_query = '';
  337.             if ( !empty($this->charset) ) {
  338.                 $collation_query = "SET NAMES '{$this->charset}'";
  339.                 if (!empty($this->collate) )
  340.                     $collation_query .= " COLLATE '{$this->collate}'";
  341.             }
  342.             
  343.             if ( !empty($collation_query) )
  344.                 $this->query($collation_query);
  345.             
  346.         }
  347.         
  348.         $this->select($dbname);
  349.     }
  350.  
  351.     /**
  352.      * PHP5 style destructor and will run when database object is destroyed.
  353.      *
  354.      * @since 2.0.8
  355.      *
  356.      * @return bool Always true
  357.      */
  358.     function __destruct() {
  359.         return true;
  360.     }
  361.  
  362.     /**
  363.      * Sets the table prefix for the WordPress tables.
  364.      *
  365.      * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
  366.      * override the WordPress users and usersmeta tables.
  367.      *
  368.      * @since 2.5.0
  369.      *
  370.      * @param string $prefix Alphanumeric name for the new prefix.
  371.      * @return string Old prefix
  372.      */
  373.     function set_prefix($prefix) {
  374.  
  375.         if ( preg_match('|[^a-z0-9_]|i', $prefix) )
  376.             return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
  377.  
  378.         $old_prefix = $this->prefix;
  379.         $this->prefix = $prefix;
  380.  
  381.         foreach ( $this->tables as $table )
  382.             $this->$table = $this->prefix . $table;
  383.  
  384.         if ( defined('CUSTOM_USER_TABLE') )
  385.             $this->users = CUSTOM_USER_TABLE;
  386.  
  387.         if ( defined('CUSTOM_USER_META_TABLE') )
  388.             $this->usermeta = CUSTOM_USER_META_TABLE;
  389.  
  390.         return $old_prefix;
  391.     }
  392.  
  393.     /**
  394.      * Selects a database using the current database connection.
  395.      *
  396.      * The database name will be changed based on the current database
  397.      * connection. On failure, the execution will bail and display an DB error.
  398.      *
  399.      * @since 0.71
  400.      *
  401.      * @param string $db MySQL database name
  402.      * @return null Always null.
  403.      */
  404.     function select($db) {
  405.         if (!@mysql_select_db($db, $this->dbh)) {
  406.             $this->ready = false;
  407.             $this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/'
  408. <h1>Can’t select database</h1>
  409. <p>We were able to connect to the database server (which means your username and password is okay) but not able to select the <code>%1$s</code> database.</p>
  410. <ul>
  411. <li>Are you sure it exists?</li>
  412. <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li>
  413. <li>On some systems the name of your database is prefixed with your username, so it would be like username_wordpress. Could that be the problem?</li>
  414. </ul>
  415. <p>If you don\'t know how to setup a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href="http://wordpress.org/support/">WordPress Support Forums</a>.</p>'/*/WP_I18N_DB_SELECT_DB*/, $db, DB_USER));
  416.             return;
  417.         }
  418.     }
  419.  
  420.     /**
  421.      * Escapes content for insertion into the database, for security
  422.      *
  423.      * @since 0.71
  424.      *
  425.      * @param string $string
  426.      * @return string query safe string
  427.      */
  428.     function escape($string) {
  429.         return addslashes( $string );
  430.         // Disable rest for now, causing problems
  431.         /*
  432.         if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' )
  433.             return mysql_escape_string( $string );
  434.         else
  435.             return mysql_real_escape_string( $string, $this->dbh );
  436.         */
  437.     }
  438.  
  439.     /**
  440.      * Escapes content by reference for insertion into the database, for security
  441.      *
  442.      * @since 2.3.0
  443.      *
  444.      * @param string $s
  445.      */
  446.     function escape_by_ref(&$s) {
  447.         $s = $this->escape($s);
  448.     }
  449.  
  450.     /**
  451.      * Prepares a SQL query for safe use, using sprintf() syntax.
  452.      *
  453.      * @link http://php.net/sprintf See for syntax to use for query string.
  454.      * @since 2.3.0
  455.      *
  456.      * @param null|string $args If string, first parameter must be query statement
  457.      * @param mixed $args,... If additional parameters, they will be set inserted into the query.
  458.      * @return null|string Sanitized query string
  459.      */
  460.     function prepare($args=null) {
  461.         if ( is_null( $args ) )
  462.             return;
  463.         $args = func_get_args();
  464.         $query = array_shift($args);
  465.         $query = str_replace("'%s'", '%s', $query); // in case someone mistakenly already singlequoted it
  466.         $query = str_replace('"%s"', '%s', $query); // doublequote unquoting
  467.         $query = str_replace('%s', "'%s'", $query); // quote the strings
  468.         array_walk($args, array(&$this, 'escape_by_ref'));
  469.         return @vsprintf($query, $args);
  470.     }
  471.  
  472.     /**
  473.      * Print SQL/DB error.
  474.      *
  475.      * @since 0.71
  476.      * @global array $EZSQL_ERROR Stores error information of query and error string
  477.      *
  478.      * @param string $str The error to display
  479.      * @return bool False if the showing of errors is disabled.
  480.      */
  481.     function print_error($str = '') {
  482.         global $EZSQL_ERROR;
  483.  
  484.         if (!$str) $str = mysql_error($this->dbh);
  485.         $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str);
  486.  
  487.         if ( $this->suppress_errors )
  488.             return false;
  489.  
  490.         if ( $caller = $this->get_caller() )
  491.             $error_str = sprintf(/*WP_I18N_DB_QUERY_ERROR_FULL*/'WordPress database error %1$s for query %2$s made by %3$s'/*/WP_I18N_DB_QUERY_ERROR_FULL*/, $str, $this->last_query, $caller);
  492.         else
  493.             $error_str = sprintf(/*WP_I18N_DB_QUERY_ERROR*/'WordPress database error %1$s for query %2$s'/*/WP_I18N_DB_QUERY_ERROR*/, $str, $this->last_query);
  494.  
  495.         $log_error = true;
  496.         if ( ! function_exists('error_log') )
  497.             $log_error = false;
  498.  
  499.         $log_file = @ini_get('error_log');
  500.         if ( !empty($log_file) && ('syslog' != $log_file) && !is_writable($log_file) )
  501.             $log_error = false;
  502.  
  503.         if ( $log_error )
  504.             @error_log($error_str, 0);
  505.  
  506.         // Is error output turned on or not..
  507.         if ( !$this->show_errors )
  508.             return false;
  509.  
  510.         $str = htmlspecialchars($str, ENT_QUOTES);
  511.         $query = htmlspecialchars($this->last_query, ENT_QUOTES);
  512.  
  513.         // If there is an error then take note of it
  514.         print "<div id='error'>
  515.         <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
  516.         <code>$query</code></p>
  517.         </div>";
  518.     }
  519.  
  520.     /**
  521.      * Enables showing of database errors.
  522.      *
  523.      * This function should be used only to enable showing of errors.
  524.      * wpdb::hide_errors() should be used instead for hiding of errors. However,
  525.      * this function can be used to enable and disable showing of database
  526.      * errors.
  527.      *
  528.      * @since 0.71
  529.      *
  530.      * @param bool $show Whether to show or hide errors
  531.      * @return bool Old value for showing errors.
  532.      */
  533.     function show_errors( $show = true ) {
  534.         $errors = $this->show_errors;
  535.         $this->show_errors = $show;
  536.         return $errors;
  537.     }
  538.  
  539.     /**
  540.      * Disables showing of database errors.
  541.      *
  542.      * @since 0.71
  543.      *
  544.      * @return bool Whether showing of errors was active or not
  545.      */
  546.     function hide_errors() {
  547.         $show = $this->show_errors;
  548.         $this->show_errors = false;
  549.         return $show;
  550.     }
  551.  
  552.     /**
  553.      * Whether to suppress database errors.
  554.      *
  555.      * @param unknown_type $suppress
  556.      * @return unknown
  557.      */
  558.     function suppress_errors( $suppress = true ) {
  559.         $errors = $this->suppress_errors;
  560.         $this->suppress_errors = $suppress;
  561.         return $errors;
  562.     }
  563.  
  564.     /**
  565.      * Kill cached query results.
  566.      *
  567.      * @since 0.71
  568.      */
  569.     function flush() {
  570.         $this->last_result = array();
  571.         $this->col_info = null;
  572.         $this->last_query = null;
  573.     }
  574.  
  575.     /**
  576.      * Perform a MySQL database query, using current database connection.
  577.      *
  578.      * More information can be found on the codex page.
  579.      *
  580.      * @since 0.71
  581.      *
  582.      * @param string $query
  583.      * @return unknown
  584.      */
  585.     function query($query) {
  586.         if ( ! $this->ready )
  587.             return false;
  588.  
  589.         // filter the query, if filters are available
  590.         // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
  591.         if ( function_exists('apply_filters') )
  592.             $query = apply_filters('query', $query);
  593.  
  594.         // initialise return
  595.         $return_val = 0;
  596.         $this->flush();
  597.  
  598.         // Log how the function was called
  599.         $this->func_call = "\$db->query(\"$query\")";
  600.  
  601.         // Keep track of the last query for debug..
  602.         $this->last_query = $query;
  603.  
  604.         // Perform the query via std mysql_query function..
  605.         if ( defined('SAVEQUERIES') && SAVEQUERIES )
  606.             $this->timer_start();
  607.  
  608.         $this->result = @mysql_query($query, $this->dbh);
  609.         ++$this->num_queries;
  610.  
  611.         if ( defined('SAVEQUERIES') && SAVEQUERIES )
  612.             $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
  613.  
  614.         // If there is an error then take note of it..
  615.         if ( $this->last_error = mysql_error($this->dbh) ) {
  616.             $this->print_error();
  617.             return false;
  618.         }
  619.  
  620.         if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {
  621.             $this->rows_affected = mysql_affected_rows($this->dbh);
  622.             // Take note of the insert_id
  623.             if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
  624.                 $this->insert_id = mysql_insert_id($this->dbh);
  625.             }
  626.             // Return number of rows affected
  627.             $return_val = $this->rows_affected;
  628.         } else {
  629.             $i = 0;
  630.             while ($i < @mysql_num_fields($this->result)) {
  631.                 $this->col_info[$i] = @mysql_fetch_field($this->result);
  632.                 $i++;
  633.             }
  634.             $num_rows = 0;
  635.             while ( $row = @mysql_fetch_object($this->result) ) {
  636.                 $this->last_result[$num_rows] = $row;
  637.                 $num_rows++;
  638.             }
  639.  
  640.             @mysql_free_result($this->result);
  641.  
  642.             // Log number of rows the query returned
  643.             $this->num_rows = $num_rows;
  644.  
  645.             // Return number of rows selected
  646.             $return_val = $this->num_rows;
  647.         }
  648.  
  649.         return $return_val;
  650.     }
  651.  
  652.     /**
  653.      * Insert an array of data into a table.
  654.      *
  655.      * @since 2.5.0
  656.      *
  657.      * @param string $table WARNING: not sanitized!
  658.      * @param array $data Should not already be SQL-escaped
  659.      * @return mixed Results of $this->query()
  660.      */
  661.     function insert($table, $data) {
  662.         $data = add_magic_quotes($data);
  663.         $fields = array_keys($data);
  664.         return $this->query("INSERT INTO $table (`" . implode('`,`',$fields) . "`) VALUES ('".implode("','",$data)."')");
  665.     }
  666.  
  667.     /**
  668.      * Update a row in the table with an array of data.
  669.      *
  670.      * @since 2.5.0
  671.      *
  672.      * @param string $table WARNING: not sanitized!
  673.      * @param array $data Should not already be SQL-escaped
  674.      * @param array $where A named array of WHERE column => value relationships.  Multiple member pairs will be joined with ANDs.  WARNING: the column names are not currently sanitized!
  675.      * @return mixed Results of $this->query()
  676.      */
  677.     function update($table, $data, $where){
  678.         $data = add_magic_quotes($data);
  679.         $bits = $wheres = array();
  680.         foreach ( array_keys($data) as $k )
  681.             $bits[] = "`$k` = '$data[$k]'";
  682.  
  683.         if ( is_array( $where ) )
  684.             foreach ( $where as $c => $v )
  685.                 $wheres[] = "$c = '" . $this->escape( $v ) . "'";
  686.         else
  687.             return false;
  688.             
  689.         return $this->query( "UPDATE $table SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ) );
  690.     }
  691.  
  692.     /**
  693.      * Retrieve one variable from the database.
  694.      *
  695.      * This combines the functionality of wpdb::get_row() and wpdb::get_col(),
  696.      * so both the column and row can be picked.
  697.      *
  698.      * It is possible to use this function without executing more queries. If
  699.      * you already made a query, you can set the $query to 'null' value and just
  700.      * retrieve either the column and row of the last query result.
  701.      *
  702.      * @since 0.71
  703.      *
  704.      * @param string $query Can be null as well, for caching
  705.      * @param int $x Column num to return
  706.      * @param int $y Row num to return
  707.      * @return mixed Database query results
  708.      */
  709.     function get_var($query=null, $x = 0, $y = 0) {
  710.         $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  711.         if ( $query )
  712.             $this->query($query);
  713.  
  714.         // Extract var out of cached results based x,y vals
  715.         if ( !empty( $this->last_result[$y] ) ) {
  716.             $values = array_values(get_object_vars($this->last_result[$y]));
  717.         }
  718.  
  719.         // If there is a value return it else return null
  720.         return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
  721.     }
  722.  
  723.     /**
  724.      * Retrieve one row from the database.
  725.      *
  726.      * @since 0.71
  727.      *
  728.      * @param string $query SQL query
  729.      * @param string $output ARRAY_A | ARRAY_N | OBJECT
  730.      * @param int $y Row num to return
  731.      * @return mixed Database query results
  732.      */
  733.     function get_row($query = null, $output = OBJECT, $y = 0) {
  734.         $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  735.         if ( $query )
  736.             $this->query($query);
  737.         else
  738.             return null;
  739.  
  740.         if ( !isset($this->last_result[$y]) )
  741.             return null;
  742.  
  743.         if ( $output == OBJECT ) {
  744.             return $this->last_result[$y] ? $this->last_result[$y] : null;
  745.         } elseif ( $output == ARRAY_A ) {
  746.             return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
  747.         } elseif ( $output == ARRAY_N ) {
  748.             return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
  749.         } else {
  750.             $this->print_error(/*WP_I18N_DB_GETROW_ERROR*/" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"/*/WP_I18N_DB_GETROW_ERROR*/);
  751.         }
  752.     }
  753.  
  754.     /**
  755.      * Retrieve one column from the database.
  756.      *
  757.      * @since 0.71
  758.      *
  759.      * @param string $query Can be null as well, for caching
  760.      * @param int $x Col num to return. Starts from 0.
  761.      * @return array Column results
  762.      */
  763.     function get_col($query = null , $x = 0) {
  764.         if ( $query )
  765.             $this->query($query);
  766.  
  767.         $new_array = array();
  768.         // Extract the column values
  769.         for ( $i=0; $i < count($this->last_result); $i++ ) {
  770.             $new_array[$i] = $this->get_var(null, $x, $i);
  771.         }
  772.         return $new_array;
  773.     }
  774.  
  775.     /**
  776.      * Retrieve an entire result set from the database.
  777.      *
  778.      * @since 0.71
  779.      *
  780.      * @param string|null $query Can also be null to pull from the cache
  781.      * @param string $output ARRAY_A | ARRAY_N | OBJECT_K | OBJECT
  782.      * @return mixed Database query results
  783.      */
  784.     function get_results($query = null, $output = OBJECT) {
  785.         $this->func_call = "\$db->get_results(\"$query\", $output)";
  786.  
  787.         if ( $query )
  788.             $this->query($query);
  789.         else
  790.             return null;
  791.  
  792.         if ( $output == OBJECT ) {
  793.             // Return an integer-keyed array of row objects
  794.             return $this->last_result;
  795.         } elseif ( $output == OBJECT_K ) {
  796.             // Return an array of row objects with keys from column 1
  797.             // (Duplicates are discarded)
  798.             foreach ( $this->last_result as $row ) {
  799.                 $key = array_shift( get_object_vars( $row ) );
  800.                 if ( !isset( $new_array[ $key ] ) )
  801.                     $new_array[ $key ] = $row;
  802.             }
  803.             return $new_array;
  804.         } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
  805.             // Return an integer-keyed array of...
  806.             if ( $this->last_result ) {
  807.                 $i = 0;
  808.                 foreach( $this->last_result as $row ) {
  809.                     if ( $output == ARRAY_N ) {
  810.                         // ...integer-keyed row arrays
  811.                         $new_array[$i] = array_values( get_object_vars( $row ) );
  812.                     } else {
  813.                         // ...column name-keyed row arrays
  814.                         $new_array[$i] = get_object_vars( $row );
  815.                     }
  816.                     ++$i;
  817.                 }
  818.                 return $new_array;
  819.             }
  820.         }
  821.     }
  822.  
  823.     /**
  824.      * Retrieve column metadata from the last query.
  825.      *
  826.      * @since 0.71
  827.      *
  828.      * @param string $info_type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill
  829.      * @param int $col_offset 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type
  830.      * @return mixed Column Results
  831.      */
  832.     function get_col_info($info_type = 'name', $col_offset = -1) {
  833.         if ( $this->col_info ) {
  834.             if ( $col_offset == -1 ) {
  835.                 $i = 0;
  836.                 foreach($this->col_info as $col ) {
  837.                     $new_array[$i] = $col->{$info_type};
  838.                     $i++;
  839.                 }
  840.                 return $new_array;
  841.             } else {
  842.                 return $this->col_info[$col_offset]->{$info_type};
  843.             }
  844.         }
  845.     }
  846.  
  847.     /**
  848.      * Starts the timer, for debugging purposes.
  849.      *
  850.      * @since 1.5.0
  851.      *
  852.      * @return bool Always returns true
  853.      */
  854.     function timer_start() {
  855.         $mtime = microtime();
  856.         $mtime = explode(' ', $mtime);
  857.         $this->time_start = $mtime[1] + $mtime[0];
  858.         return true;
  859.     }
  860.  
  861.     /**
  862.      * Stops the debugging timer.
  863.      *
  864.      * @since 1.5.0
  865.      *
  866.      * @return int Total time spent on the query, in milliseconds
  867.      */
  868.     function timer_stop() {
  869.         $mtime = microtime();
  870.         $mtime = explode(' ', $mtime);
  871.         $time_end = $mtime[1] + $mtime[0];
  872.         $time_total = $time_end - $this->time_start;
  873.         return $time_total;
  874.     }
  875.  
  876.     /**
  877.      * Wraps fatal errors in a nice header and footer and dies.
  878.      *
  879.      * @since 1.5.0
  880.      *
  881.      * @param string $message
  882.      * @return unknown
  883.      */
  884.     function bail($message) {
  885.         if ( !$this->show_errors ) {
  886.             if ( class_exists('WP_Error') )
  887.                 $this->error = new WP_Error('500', $message);
  888.             else
  889.                 $this->error = $message;
  890.             return false;
  891.         }
  892.         wp_die($message);
  893.     }
  894.  
  895.     /**
  896.      * Whether or not MySQL database is minimal required version.
  897.      *
  898.      * @since 2.5.0
  899.      * @uses $wp_version
  900.      *
  901.      * @return WP_Error
  902.      */
  903.     function check_database_version()
  904.     {
  905.         global $wp_version;
  906.         // Make sure the server has MySQL 4.0
  907.         $mysql_version = preg_replace('|[^0-9\.]|', '', @mysql_get_server_info($this->dbh));
  908.         if ( version_compare($mysql_version, '4.0.0', '<') )
  909.             return new WP_Error('database_version',sprintf(__('<strong>ERROR</strong>: WordPress %s requires MySQL 4.0.0 or higher'), $wp_version));
  910.     }
  911.  
  912.     /**
  913.      * Whether of not the database version supports collation.
  914.      *
  915.      * Called when WordPress is generating the table scheme.
  916.      *
  917.      * @since 2.5.0
  918.      *
  919.      * @return bool True if collation is supported, false if version does not
  920.      */
  921.     function supports_collation()
  922.     {
  923.         return ( version_compare(mysql_get_server_info($this->dbh), '4.1.0', '>=') );
  924.     }
  925.  
  926.     /**
  927.      * Retrieve the name of the function that called wpdb.
  928.      *
  929.      * Requires PHP 4.3 and searches up the list of functions until it reaches
  930.      * the one that would most logically had called this method.
  931.      *
  932.      * @since 2.5.0
  933.      *
  934.      * @return string The name of the calling function
  935.      */
  936.     function get_caller() {
  937.         // requires PHP 4.3+
  938.         if ( !is_callable('debug_backtrace') )
  939.             return '';
  940.  
  941.         $bt = debug_backtrace();
  942.         $caller = '';
  943.  
  944.         foreach ( $bt as $trace ) {
  945.             if ( @$trace['class'] == __CLASS__ )
  946.                 continue;
  947.             elseif ( strtolower(@$trace['function']) == 'call_user_func_array' )
  948.                 continue;
  949.             elseif ( strtolower(@$trace['function']) == 'apply_filters' )
  950.                 continue;
  951.             elseif ( strtolower(@$trace['function']) == 'do_action' )
  952.                 continue;
  953.  
  954.             $caller = $trace['function'];
  955.             break;
  956.         }
  957.         return $caller;
  958.     }
  959.  
  960. }
  961.  
  962. if ( ! isset($wpdb) ) {
  963.     /**
  964.      * WordPress Database Object, if it isn't set already in wp-content/wpdb.php
  965.      * @global object $wpdb Creates a new wpdb object based on wp-config.php Constants for the database
  966.      * @since 0.71
  967.      */
  968.     $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
  969. }
  970. ?>
  971.