Make WordPress Core

Changeset 1178


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

Just an indenting thing that was bugging me.

File:
1 edited

Legend:

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

    r964 r1178  
    11<?php
    2     // ==================================================================
    3     //  Author: Justin Vincent ([email protected])
    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');
    12     define('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.
    18 
    19     class wpdb {
    20 
    21         var $debug_called;
    22         var $vardump_called;
    23         var $show_errors = true;
    24         var $querycount;
    25 
    26         // ==================================================================
    27         //  DB Constructor - connects to the server and selects a database
    28 
    29         function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
    30             $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword);
    31             if ( ! $this->dbh ) {
    32                 die("<div>
    33                 <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>
    34                 <ul>
    35                 <li>Are you sure you have the correct user/password?</li>
    36                 <li>Are you sure that you have typed the correct hostname?</li>
    37                 <li>Are you sure that the database server is running?</li>
    38                 </ul>
    39                 <p><a href='http://wordpress.org/support/'>WordPress Support Forums</a></p>
    40                 </div>");
     2// ==================================================================
     3//  Author: Justin Vincent ([email protected])
     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
     11define('EZSQL_VERSION', '1.21');
     12define('OBJECT', 'OBJECT', true);
     13define('ARRAY_A', 'ARRAY_A', true);
     14define('ARRAY_N', 'ARRAY_N', true);
     15define('SAVEQUERIES', true);
     16
     17//  The Main Class, renamed to avoid conflicts.
     18
     19class wpdb {
     20
     21    var $debug_called;
     22    var $vardump_called;
     23    var $show_errors = true;
     24    var $querycount;
     25
     26    // ==================================================================
     27    //  DB Constructor - connects to the server and selects a database
     28
     29    function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
     30        $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword);
     31        if ( ! $this->dbh ) {
     32            die("<div>
     33            <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>
     34            <ul>
     35            <li>Are you sure you have the correct user/password?</li>
     36            <li>Are you sure that you have typed the correct hostname?</li>
     37            <li>Are you sure that the database server is running?</li>
     38            </ul>
     39            <p><a href='http://wordpress.org/support/'>WordPress Support Forums</a></p>
     40            </div>");
     41        }
     42
     43        $this->select($dbname);
     44        $this->querycount = 0;
     45    }
     46
     47    // ==================================================================
     48    //  Select a DB (if another one needs to be selected)
     49
     50    function select($db) {
     51        if ( !@mysql_select_db($db,$this->dbh)) {
     52            die("
     53            <p>We're having a little trouble selecting the proper database for WordPress.</p>
     54            <ul>
     55            <li>Are you sure it exists?</li>
     56            <li>Your database name is currently specified as <code>" . DB_NAME ."</code>. Is this correct?</li>
     57            <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>
     58            </ul>
     59            <p><a href='http://wordpress.org/support/'>WordPress Support Forums</a></p>");
     60        }
     61    }
     62
     63    // ====================================================================
     64    //  Format a string correctly for safe insert under all PHP conditions
     65   
     66    function escape($str) {
     67        return mysql_escape_string(stripslashes($str));             
     68    }
     69
     70    // ==================================================================
     71    //  Print SQL/DB error.
     72
     73    function print_error($str = '') {
     74        // All errors go to the global error array $EZSQL_ERROR..
     75        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                        );
     86
     87        // Is error output turned on or not..
     88        if ( $this->show_errors ) {
     89            // If there is an error then take note of it
     90            print "<div id='error'>
     91            <p><strong>SQL/DB Error:</strong><br />
     92            [<span style='color: #007;'>$str</span>]<br />
     93            <code>$this->last_query</code></p>
     94            </div>";
     95        } else {
     96            return false;   
     97        }
     98
     99    }
     100
     101    // ==================================================================
     102    //  Turn error handling on or off..
     103
     104    function show_errors() {
     105        $this->show_errors = true;
     106    }
     107   
     108    function hide_errors() {
     109        $this->show_errors = false;
     110    }
     111
     112    // ==================================================================
     113    //  Kill cached query results
     114
     115    function flush() {
     116        // Get rid of these
     117        $this->last_result = null;
     118        $this->col_info = null;
     119        $this->last_query = null;
     120
     121    }
     122
     123    // ==================================================================
     124    //  Basic Query - see docs for more detail
     125
     126    function query($query) {
     127
     128        // Flush cached values..
     129        $this->flush();
     130
     131        // Log how the function was called
     132        $this->func_call = "\$db->query(\"$query\")";
     133
     134        // Keep track of the last query for debug..
     135        $this->last_query = $query;
     136
     137        // Perform the query via std mysql_query function..
     138        $this->result = mysql_query($query, $this->dbh);
     139        ++$this->querycount;
     140        if (SAVEQUERIES) {
     141            $this->savedqueries[] = $query;
     142        }
     143
     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;
    41158            }
    42 
    43             $this->select($dbname);
    44             $this->querycount = 0;
    45         }
    46 
    47         // ==================================================================
    48         //  Select a DB (if another one needs to be selected)
    49 
    50         function select($db) {
    51             if ( !@mysql_select_db($db,$this->dbh)) {
    52                 die("
    53                 <p>We're having a little trouble selecting the proper database for WordPress.</p>
    54                 <ul>
    55                 <li>Are you sure it exists?</li>
    56                 <li>Your database name is currently specified as <code>" . DB_NAME ."</code>. Is this correct?</li>
    57                 <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>
    58                 </ul>
    59                 <p><a href='http://wordpress.org/support/'>WordPress Support Forums</a></p>");
     159        }
     160
     161        if ( mysql_error() ) { // If there is an error then take note of it..
     162            $this->print_error();
     163        } 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;
    60200            }
    61201        }
    62 
    63         // ====================================================================
    64         //  Format a string correctly for safe insert under all PHP conditions
    65        
    66         function escape($str) {
    67             return mysql_escape_string(stripslashes($str));             
    68         }
    69 
    70         // ==================================================================
    71         //  Print SQL/DB error.
    72 
    73         function print_error($str = '') {
    74             // All errors go to the global error array $EZSQL_ERROR..
    75             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                             );
    86 
    87             // Is error output turned on or not..
    88             if ( $this->show_errors ) {
    89                 // If there is an error then take note of it
    90                 print "<div id='error'>
    91                 <p><strong>SQL/DB Error:</strong><br />
    92                 [<span style='color: #007;'>$str</span>]<br />
    93                 <code>$this->last_query</code></p>
    94                 </div>";
     202    }
     203
     204    // ==================================================================
     205    //  Get one variable from the DB - see docs for more detail
     206
     207    function get_var($query=null, $x=0, $y=0) {
     208
     209        // Log how the function was called
     210        $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        }
     216
     217        // Extract var out of cached results based x,y vals
     218        if ( $this->last_result[$y] ) {
     219            $values = array_values(get_object_vars($this->last_result[$y]));
     220        }
     221
     222        // If there is a value return it else return null
     223        return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
     224    }
     225
     226    // ==================================================================
     227    //  Get one row from the DB - see docs for more detail
     228
     229    function get_row($query=null, $output=OBJECT, $y=0) {
     230
     231        // Log how the function was called
     232        $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..
     240        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 {
     253            $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
     254        }
     255
     256    }
     257
     258    // ==================================================================
     259    //  Function to get 1 column from the cached result set based in X index
     260    // se docs for usage and info
     261
     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        }
     267        // Extract the column values
     268        for ( $i=0; $i < count($this->last_result); $i++ ) {
     269            $new_array[$i] = $this->get_var(null,$x,$i);
     270        }
     271        return $new_array;
     272    }
     273
     274    // ==================================================================
     275    // Return the the query as a result set - see docs for more details
     276
     277    function get_results($query=null, $output = OBJECT){
     278
     279        // Log how the function was called
     280        $this->func_call = "\$db->get_results(\"$query\", $output)";
     281
     282        // If there is a query then perform it if not then use cached results..
     283        if ( $query ) {
     284            $this->query($query);
     285        }
     286
     287        // Send back array of objects. Each row is an object
     288        if ( $output == OBJECT ) {
     289            return $this->last_result;
     290        } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
     291            if ( $this->last_result ) {
     292                $i=0;
     293                foreach( $this->last_result as $row ) {
     294                    $new_array[$i] = (array) $row;
     295                    if ( $output == ARRAY_N ) {
     296                        $new_array[$i] = array_values($new_array[$i]);
     297                    }
     298
     299                    $i++;
     300                }
     301
     302                return $new_array;
    95303            } else {
    96                 return false;   
     304                return null;
    97305            }
    98 
    99         }
    100 
    101         // ==================================================================
    102         //  Turn error handling on or off..
    103 
    104         function show_errors() {
    105             $this->show_errors = true;
    106         }
    107        
    108         function hide_errors() {
    109             $this->show_errors = false;
    110         }
    111 
    112         // ==================================================================
    113         //  Kill cached query results
    114 
    115         function flush() {
    116             // Get rid of these
    117             $this->last_result = null;
    118             $this->col_info = null;
    119             $this->last_query = null;
    120 
    121         }
    122 
    123         // ==================================================================
    124         //  Basic Query - see docs for more detail
    125 
    126         function query($query) {
    127 
    128             // Flush cached values..
    129             $this->flush();
    130 
    131             // Log how the function was called
    132             $this->func_call = "\$db->query(\"$query\")";
    133 
    134             // Keep track of the last query for debug..
    135             $this->last_query = $query;
    136 
    137             // Perform the query via std mysql_query function..
    138             $this->result = mysql_query($query, $this->dbh);
    139             ++$this->querycount;
    140             if (SAVEQUERIES) {
    141                 $this->savedqueries[] = $query;
     306        }
     307    }
     308
     309
     310    // ==================================================================
     311    // Function to get column meta data info pertaining to the last query
     312    // see docs for more info and usage
     313
     314    function get_col_info($info_type='name', $col_offset=-1) {
     315
     316        if ( $this->col_info ) {
     317            if ( $col_offset == -1 ) {
     318                $i=0;
     319                foreach($this->col_info as $col ) {
     320                    $new_array[$i] = $col->{$info_type};
     321                    $i++;
     322                }
     323                return $new_array;
     324            } else {
     325                return $this->col_info[$col_offset]->{$info_type};
    142326            }
    143327
    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..
    162                 $this->print_error();
    163             } 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             }
    202         }
    203 
    204         // ==================================================================
    205         //  Get one variable from the DB - see docs for more detail
    206 
    207         function get_var($query=null, $x=0, $y=0) {
    208 
    209             // Log how the function was called
    210             $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             }
    216 
    217             // Extract var out of cached results based x,y vals
    218             if ( $this->last_result[$y] ) {
    219                 $values = array_values(get_object_vars($this->last_result[$y]));
    220             }
    221 
    222             // If there is a value return it else return null
    223             return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
    224         }
    225 
    226         // ==================================================================
    227         //  Get one row from the DB - see docs for more detail
    228 
    229         function get_row($query=null, $output=OBJECT, $y=0) {
    230 
    231             // Log how the function was called
    232             $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..
    240             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 {
    253                 $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
    254             }
    255 
    256         }
    257 
    258         // ==================================================================
    259         //  Function to get 1 column from the cached result set based in X index
    260         // se docs for usage and info
    261 
    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             }
    267             // Extract the column values
    268             for ( $i=0; $i < count($this->last_result); $i++ ) {
    269                 $new_array[$i] = $this->get_var(null,$x,$i);
    270             }
    271             return $new_array;
    272         }
    273 
    274         // ==================================================================
    275         // Return the the query as a result set - see docs for more details
    276 
    277         function get_results($query=null, $output = OBJECT){
    278 
    279             // Log how the function was called
    280             $this->func_call = "\$db->get_results(\"$query\", $output)";
    281 
    282             // If there is a query then perform it if not then use cached results..
    283             if ( $query ) {
    284                 $this->query($query);
    285             }
    286 
    287             // Send back array of objects. Each row is an object
    288             if ( $output == OBJECT ) {
    289                 return $this->last_result;
    290             } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
    291                 if ( $this->last_result ) {
    292                     $i=0;
    293                     foreach( $this->last_result as $row ) {
    294                         $new_array[$i] = (array) $row;
    295                         if ( $output == ARRAY_N ) {
    296                             $new_array[$i] = array_values($new_array[$i]);
    297                         }
    298 
    299                         $i++;
    300                     }
    301 
    302                     return $new_array;
    303                 } else {
    304                     return null;
    305                 }
    306             }
    307         }
    308 
    309 
    310         // ==================================================================
    311         // Function to get column meta data info pertaining to the last query
    312         // see docs for more info and usage
    313 
    314         function get_col_info($info_type='name', $col_offset=-1) {
    315 
    316             if ( $this->col_info ) {
    317                 if ( $col_offset == -1 ) {
    318                     $i=0;
    319                     foreach($this->col_info as $col ) {
    320                         $new_array[$i] = $col->{$info_type};
    321                         $i++;
    322                     }
    323                     return $new_array;
    324                 } else {
    325                     return $this->col_info[$col_offset]->{$info_type};
    326                 }
    327 
    328             }
    329 
    330         }
    331 
    332     }
     328        }
     329
     330    }
     331
     332}
    333333
    334334$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
Note: See TracChangeset for help on using the changeset viewer.