Make WordPress Core

Changeset 1180


Ignore:
Timestamp:
04/26/2004 02:54:06 AM (20 years ago)
Author:
saxmatt
Message:

Updated to use the latest and greatest.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/wp-db.php

    r1178 r1180  
    11<?php
    2 // ==================================================================
    3 //  Author: Justin Vincent (justin@visunet.ie)
    4 //  Web:    http://php.justinvincent.com
    5 //  Name:   ezSQL
    6 //  Desc:   Class to make it very easy to deal with mySQL database connections.
    7 //  WordPress is using this class to make the code cleaner and faster.
    8 //  We highly recommend it.
    9 //  We have modified the HTML it returns slightly.
    10 
    11 define('EZSQL_VERSION', '1.21');
     2//  WordPress DB Class
     3
     4//  ORIGINAL CODE FROM:
     5//  Justin Vincent (justin@visunet.ie)
     6//  http://php.justinvincent.com
     7
     8define('EZSQL_VERSION', 'WP1.25');
    129define('OBJECT', 'OBJECT', true);
    13 define('ARRAY_A', 'ARRAY_A', true);
    14 define('ARRAY_N', 'ARRAY_N', true);
    15 define('SAVEQUERIES', true);
    16 
    17 //  The Main Class, renamed to avoid conflicts.
     10define('ARRAY_A', 'ARRAY_A', false);
     11define('ARRAY_N', 'ARRAY_N', false);
     12if (!defined('SAVEQUERIES'))
     13    define('SAVEQUERIES', false);
    1814
    1915class wpdb {
    2016
    21     var $debug_called;
    22     var $vardump_called;
    2317    var $show_errors = true;
    24     var $querycount;
     18    var $num_queries = 0;   
     19    var $last_query;
     20    var $col_info;
    2521
    2622    // ==================================================================
     
    2925    function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
    3026        $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword);
    31         if ( ! $this->dbh ) {
     27        if (!$this->dbh) {
    3228            die("<div>
    3329            <p><strong>Error establishing a database connection!</strong> This probably means that the connection information in your <code>wp-config.php</code> file is incorrect. Double check it and try again.</p>
     
    4238
    4339        $this->select($dbname);
    44         $this->querycount = 0;
    4540    }
    4641
     
    4944
    5045    function select($db) {
    51         if ( !@mysql_select_db($db,$this->dbh)) {
     46        if (!@mysql_select_db($db,$this->dbh)) {
    5247            die("
    5348            <p>We're having a little trouble selecting the proper database for WordPress.</p>
     
    7267
    7368    function print_error($str = '') {
    74         // All errors go to the global error array $EZSQL_ERROR..
    7569        global $EZSQL_ERROR;
    76 
    77         // If no special error string then use mysql default..
    78         if ( !$str ) $str = mysql_error();
    79        
    80         // Log this error to the global array..
    81         $EZSQL_ERROR[] = array
    82                         (
    83                             'query' => $this->last_query,
    84                             'error_str'  => $str
    85                         );
     70        if (!$str) $str = mysql_error();
     71        $EZSQL_ERROR[] =
     72        array ('query' => $this->last_query, 'error_str' => $str);
    8673
    8774        // Is error output turned on or not..
     
    8976            // If there is an error then take note of it
    9077            print "<div id='error'>
    91             <p><strong>SQL/DB Error:</strong><br />
    92             [<span style='color: #007;'>$str</span>]<br />
     78            <p><strong>Database error:</strong> [$str]<br />
    9379            <code>$this->last_query</code></p>
    9480            </div>";
     
    9682            return false;   
    9783        }
    98 
    9984    }
    10085
     
    11499
    115100    function flush() {
    116         // Get rid of these
    117101        $this->last_result = null;
    118102        $this->col_info = null;
    119103        $this->last_query = null;
    120 
    121104    }
    122105
     
    125108
    126109    function query($query) {
    127 
    128         // Flush cached values..
     110        // initialise return
     111        $return_val = 0;
    129112        $this->flush();
    130113
     
    136119
    137120        // Perform the query via std mysql_query function..
    138         $this->result = mysql_query($query, $this->dbh);
    139         ++$this->querycount;
     121        $this->result = @mysql_query($query,$this->dbh);
     122        ++$this->num_queries;
    140123        if (SAVEQUERIES) {
    141124            $this->savedqueries[] = $query;
    142125        }
    143126
    144         // If there was an insert, delete or update see how many rows were affected
    145         // (Also, If there there was an insert take note of the insert_id
    146         $query_type = array('insert','delete','update','replace');
    147 
    148         // loop through the above array
    149         foreach ( $query_type as $word ) {
    150             // This is true if the query starts with insert, delete or update
    151             if ( preg_match("/^\\s*$word /i",$query) ) {
    152                 $this->rows_affected = mysql_affected_rows();
    153                 // This gets the insert ID
    154                 if ( $word == 'insert' || $word == 'replace' ) {
    155                     $this->insert_id = mysql_insert_id($this->dbh);
    156                 }
    157                 $this->result = false;
    158             }
    159         }
    160 
    161         if ( mysql_error() ) { // If there is an error then take note of it..
     127        // If there is an error then take note of it..
     128        if ( mysql_error() ) {
    162129            $this->print_error();
     130            return false;
     131        }
     132
     133        if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {
     134            $this->rows_affected = mysql_affected_rows();
     135            // Take note of the insert_id
     136            if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
     137                $this->insert_id = mysql_insert_id($this->dbh);
     138            }
     139            // Return number of rows affected
     140            $return_val = $this->rows_affected;
    163141        } else {
    164             // In other words if this was a select statement..
    165             if ( $this->result ) {
    166                 // =======================================================
    167                 // Take note of column info
    168 
    169                 $i=0;
    170                 while ($i < @mysql_num_fields($this->result)) {
    171                     $this->col_info[$i] = @mysql_fetch_field($this->result);
    172                     $i++;
    173                 }
    174 
    175                 // =======================================================
    176                 // Store Query Results
    177 
    178                 $i=0;
    179                 while ( $row = @mysql_fetch_object($this->result) ) {
    180                     // Store relults as an objects within main array
    181                     $this->last_result[$i] = $row;
    182                     $i++;
    183                 }
    184 
    185                 // Log number of rows the query returned
    186                 $this->num_rows = $i;
    187 
    188                 @mysql_free_result($this->result);
    189 
    190 
    191                 // If there were results then return true for $db->query
    192                 if ( $i ) {
    193                     return true;
    194                 } else {
    195                     return false;
    196                 }
    197 
    198             } else { // Update insert etc. was good..
    199                 return true;
    200             }
    201         }
     142            $i = 0;
     143            while ($i < @mysql_num_fields($this->result)) {
     144                $this->col_info[$i] = @mysql_fetch_field($this->result);
     145                $i++;
     146            }
     147            $num_rows = 0;
     148            while ( $row = @mysql_fetch_object($this->result) ) {
     149                $this->last_result[$num_rows] = $row;
     150                $num_rows++;
     151            }
     152
     153            @mysql_free_result($this->result);
     154
     155            // Log number of rows the query returned
     156            $this->num_rows = $num_rows;
     157           
     158            // Return number of rows selected
     159            $return_val = $this->num_rows;
     160        }
     161
     162        // If debug ALL queries
     163        $this->trace || $this->debug_all ? $this->debug() : null ;
     164
     165        return $return_val;
    202166    }
    203167
     
    205169    //  Get one variable from the DB - see docs for more detail
    206170
    207     function get_var($query=null, $x=0, $y=0) {
    208 
    209         // Log how the function was called
     171    function get_var($query=null, $x = 0, $y = 0) {
    210172        $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
    211 
    212         // If there is a query then perform it if not then use cached results..
    213         if ( $query ) {
    214             $this->query($query);
    215         }
     173        if ( $query )
     174            $this->query($query);
    216175
    217176        // Extract var out of cached results based x,y vals
     
    227186    //  Get one row from the DB - see docs for more detail
    228187
    229     function get_row($query=null, $output=OBJECT, $y=0) {
    230 
    231         // Log how the function was called
     188    function get_row($query = null, $output = OBJECT, $y = 0) {
    232189        $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
    233 
    234         // If there is a query then perform it if not then use cached results..
    235         if ( $query ) {
    236             $this->query($query);
    237         }
    238 
    239         // If the output is an object then return object using the row offset..
     190        if ( $query )
     191            $this->query($query);
     192
    240193        if ( $output == OBJECT ) {
    241             return $this->last_result[$y]?$this->last_result[$y]:null;
    242         }
    243         // If the output is an associative array then return row as such..
    244         elseif ( $output == ARRAY_A ) {
    245             return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null;
    246         }
    247         // If the output is an numerical array then return row as such..
    248         elseif ( $output == ARRAY_N ) {
    249             return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null;
    250         }
    251         // If invalid output type was specified..
    252         else {
     194            return $this->last_result[$y] ? $this->last_result[$y] : null;
     195        } elseif ( $output == ARRAY_A ) {
     196            return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
     197        } elseif ( $output == ARRAY_N ) {
     198            return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
     199        } else {
    253200            $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
    254201        }
    255 
    256202    }
    257203
     
    260206    // se docs for usage and info
    261207
    262     function get_col($query=null,$x=0) {
    263         // If there is a query then perform it if not then use cached results..
    264         if ( $query ) {
    265             $this->query($query);
    266         }
     208    function get_col($query = null , $x = 0) {
     209        if ( $query )
     210            $this->query($query);
     211
    267212        // Extract the column values
    268213        for ( $i=0; $i < count($this->last_result); $i++ ) {
    269             $new_array[$i] = $this->get_var(null,$x,$i);
     214            $new_array[$i] = $this->get_var(null, $x, $i);
    270215        }
    271216        return $new_array;
     
    275220    // Return the the query as a result set - see docs for more details
    276221
    277     function get_results($query=null, $output = OBJECT){
    278 
    279         // Log how the function was called
     222    function get_results($query = null, $output = OBJECT) {
    280223        $this->func_call = "\$db->get_results(\"$query\", $output)";
    281224
    282         // If there is a query then perform it if not then use cached results..
    283         if ( $query ) {
    284             $this->query($query);
    285         }
     225        if ( $query )
     226            $this->query($query);
    286227
    287228        // Send back array of objects. Each row is an object
     
    290231        } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
    291232            if ( $this->last_result ) {
    292                 $i=0;
     233                $i = 0;
    293234                foreach( $this->last_result as $row ) {
    294235                    $new_array[$i] = (array) $row;
     
    296237                        $new_array[$i] = array_values($new_array[$i]);
    297238                    }
    298 
    299239                    $i++;
    300240                }
    301 
    302241                return $new_array;
    303242            } else {
     
    312251    // see docs for more info and usage
    313252
    314     function get_col_info($info_type='name', $col_offset=-1) {
    315 
     253    function get_col_info($info_type = 'name', $col_offset = -1) {
    316254        if ( $this->col_info ) {
    317255            if ( $col_offset == -1 ) {
    318                 $i=0;
     256                $i = 0;
    319257                foreach($this->col_info as $col ) {
    320258                    $new_array[$i] = $col->{$info_type};
     
    325263                return $this->col_info[$col_offset]->{$info_type};
    326264            }
    327 
    328         }
    329 
    330     }
    331 
     265        }
     266    }
    332267}
    333268
Note: See TracChangeset for help on using the changeset viewer.