487 * @param string $dbpassword MySQL database password |
532 * @param string $dbpassword MySQL database password |
488 * @param string $dbname MySQL database name |
533 * @param string $dbname MySQL database name |
489 * @param string $dbhost MySQL database host |
534 * @param string $dbhost MySQL database host |
490 */ |
535 */ |
491 function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { |
536 function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { |
492 register_shutdown_function( array( &$this, '__destruct' ) ); |
537 register_shutdown_function( array( $this, '__destruct' ) ); |
493 |
538 |
494 if ( WP_DEBUG ) |
539 if ( WP_DEBUG ) |
495 $this->show_errors(); |
540 $this->show_errors(); |
496 |
541 |
497 $this->init_charset(); |
542 $this->init_charset(); |
511 * @since 2.0.8 |
556 * @since 2.0.8 |
512 * @return bool true |
557 * @return bool true |
513 */ |
558 */ |
514 function __destruct() { |
559 function __destruct() { |
515 return true; |
560 return true; |
|
561 } |
|
562 |
|
563 /** |
|
564 * PHP5 style magic getter, used to lazy-load expensive data. |
|
565 * |
|
566 * @since 3.5.0 |
|
567 * |
|
568 * @param string $name The private member to get, and optionally process |
|
569 * @return mixed The private member |
|
570 */ |
|
571 function __get( $name ) { |
|
572 if ( 'col_info' == $name ) |
|
573 $this->load_col_info(); |
|
574 |
|
575 return $this->$name; |
|
576 } |
|
577 |
|
578 /** |
|
579 * Magic function, for backwards compatibility |
|
580 * |
|
581 * @since 3.5.0 |
|
582 * |
|
583 * @param string $name The private member to set |
|
584 * @param mixed $value The value to set |
|
585 */ |
|
586 function __set( $name, $value ) { |
|
587 $this->$name = $value; |
|
588 } |
|
589 |
|
590 /** |
|
591 * Magic function, for backwards compatibility |
|
592 * |
|
593 * @since 3.5.0 |
|
594 * |
|
595 * @param string $name The private member to check |
|
596 * |
|
597 * @return bool If the member is set or not |
|
598 */ |
|
599 function __isset( $name ) { |
|
600 return isset( $this->$name ); |
|
601 } |
|
602 |
|
603 /** |
|
604 * Magic function, for backwards compatibility |
|
605 * |
|
606 * @since 3.5.0 |
|
607 * |
|
608 * @param string $name The private member to unset |
|
609 */ |
|
610 function __unset( $name ) { |
|
611 unset( $this->$name ); |
516 } |
612 } |
517 |
613 |
518 /** |
614 /** |
519 * Set $this->charset and $this->collate |
615 * Set $this->charset and $this->collate |
520 * |
616 * |
888 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like |
985 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like |
889 * {@link http://php.net/sprintf sprintf()}. |
986 * {@link http://php.net/sprintf sprintf()}. |
890 * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string |
987 * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string |
891 * if there was something to prepare |
988 * if there was something to prepare |
892 */ |
989 */ |
893 function prepare( $query = null ) { // ( $query, *$args ) |
990 function prepare( $query, $args ) { |
894 if ( is_null( $query ) ) |
991 if ( is_null( $query ) ) |
895 return; |
992 return; |
896 |
993 |
897 $args = func_get_args(); |
994 $args = func_get_args(); |
898 array_shift( $args ); |
995 array_shift( $args ); |
899 // If args were passed as an array (as in vsprintf), move them up |
996 // If args were passed as an array (as in vsprintf), move them up |
900 if ( isset( $args[0] ) && is_array($args[0]) ) |
997 if ( isset( $args[0] ) && is_array($args[0]) ) |
901 $args = $args[0]; |
998 $args = $args[0]; |
902 $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it |
999 $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it |
903 $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting |
1000 $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting |
|
1001 $query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware |
904 $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s |
1002 $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s |
905 array_walk( $args, array( &$this, 'escape_by_ref' ) ); |
1003 array_walk( $args, array( $this, 'escape_by_ref' ) ); |
906 return @vsprintf( $query, $args ); |
1004 return @vsprintf( $query, $args ); |
907 } |
1005 } |
908 |
1006 |
909 /** |
1007 /** |
910 * Print SQL/DB error. |
1008 * Print SQL/DB error. |
930 if ( $caller = $this->get_caller() ) |
1028 if ( $caller = $this->get_caller() ) |
931 $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str, $this->last_query, $caller ); |
1029 $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str, $this->last_query, $caller ); |
932 else |
1030 else |
933 $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s' ), $str, $this->last_query ); |
1031 $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s' ), $str, $this->last_query ); |
934 |
1032 |
935 if ( function_exists( 'error_log' ) |
1033 error_log( $error_str ); |
936 && ( $log_file = @ini_get( 'error_log' ) ) |
|
937 && ( 'syslog' == $log_file || @is_writable( $log_file ) ) |
|
938 ) |
|
939 @error_log( $error_str ); |
|
940 |
1034 |
941 // Are we showing errors? |
1035 // Are we showing errors? |
942 if ( ! $this->show_errors ) |
1036 if ( ! $this->show_errors ) |
943 return false; |
1037 return false; |
944 |
1038 |
1032 */ |
1129 */ |
1033 function db_connect() { |
1130 function db_connect() { |
1034 |
1131 |
1035 $this->is_mysql = true; |
1132 $this->is_mysql = true; |
1036 |
1133 |
|
1134 $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; |
|
1135 $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; |
|
1136 |
1037 if ( WP_DEBUG ) { |
1137 if ( WP_DEBUG ) { |
1038 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true ); |
1138 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); |
1039 } else { |
1139 } else { |
1040 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true ); |
1140 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); |
1041 } |
1141 } |
1042 |
1142 |
1043 if ( !$this->dbh ) { |
1143 if ( !$this->dbh ) { |
1044 wp_load_translations_early(); |
1144 wp_load_translations_early(); |
1045 $this->bail( sprintf( __( " |
1145 $this->bail( sprintf( __( " |
1102 if ( $this->last_error = mysql_error( $this->dbh ) ) { |
1202 if ( $this->last_error = mysql_error( $this->dbh ) ) { |
1103 $this->print_error(); |
1203 $this->print_error(); |
1104 return false; |
1204 return false; |
1105 } |
1205 } |
1106 |
1206 |
1107 if ( preg_match( '/^\s*(create|alter|truncate|drop) /i', $query ) ) { |
1207 if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { |
1108 $return_val = $this->result; |
1208 $return_val = $this->result; |
1109 } elseif ( preg_match( '/^\s*(insert|delete|update|replace) /i', $query ) ) { |
1209 } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { |
1110 $this->rows_affected = mysql_affected_rows( $this->dbh ); |
1210 $this->rows_affected = mysql_affected_rows( $this->dbh ); |
1111 // Take note of the insert_id |
1211 // Take note of the insert_id |
1112 if ( preg_match( '/^\s*(insert|replace) /i', $query ) ) { |
1212 if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { |
1113 $this->insert_id = mysql_insert_id($this->dbh); |
1213 $this->insert_id = mysql_insert_id($this->dbh); |
1114 } |
1214 } |
1115 // Return number of rows affected |
1215 // Return number of rows affected |
1116 $return_val = $this->rows_affected; |
1216 $return_val = $this->rows_affected; |
1117 } else { |
1217 } else { |
1118 $i = 0; |
|
1119 while ( $i < @mysql_num_fields( $this->result ) ) { |
|
1120 $this->col_info[$i] = @mysql_fetch_field( $this->result ); |
|
1121 $i++; |
|
1122 } |
|
1123 $num_rows = 0; |
1218 $num_rows = 0; |
1124 while ( $row = @mysql_fetch_object( $this->result ) ) { |
1219 while ( $row = @mysql_fetch_object( $this->result ) ) { |
1125 $this->last_result[$num_rows] = $row; |
1220 $this->last_result[$num_rows] = $row; |
1126 $num_rows++; |
1221 $num_rows++; |
1127 } |
1222 } |
1128 |
|
1129 @mysql_free_result( $this->result ); |
|
1130 |
1223 |
1131 // Log number of rows the query returned |
1224 // Log number of rows the query returned |
1132 // and return number of rows selected |
1225 // and return number of rows selected |
1133 $this->num_rows = $num_rows; |
1226 $this->num_rows = $num_rows; |
1134 $return_val = $num_rows; |
1227 $return_val = $num_rows; |
1454 } |
1547 } |
1455 return null; |
1548 return null; |
1456 } |
1549 } |
1457 |
1550 |
1458 /** |
1551 /** |
|
1552 * Load the column metadata from the last query. |
|
1553 * |
|
1554 * @since 3.5.0 |
|
1555 * |
|
1556 * @access protected |
|
1557 */ |
|
1558 protected function load_col_info() { |
|
1559 if ( $this->col_info ) |
|
1560 return; |
|
1561 |
|
1562 for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { |
|
1563 $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); |
|
1564 } |
|
1565 } |
|
1566 |
|
1567 /** |
1459 * Retrieve column metadata from the last query. |
1568 * Retrieve column metadata from the last query. |
1460 * |
1569 * |
1461 * @since 0.71 |
1570 * @since 0.71 |
1462 * |
1571 * |
1463 * @param string $info_type Optional. Type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill |
1572 * @param string $info_type Optional. Type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill |
1464 * @param int $col_offset Optional. 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 |
1573 * @param int $col_offset Optional. 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 |
1465 * @return mixed Column Results |
1574 * @return mixed Column Results |
1466 */ |
1575 */ |
1467 function get_col_info( $info_type = 'name', $col_offset = -1 ) { |
1576 function get_col_info( $info_type = 'name', $col_offset = -1 ) { |
|
1577 $this->load_col_info(); |
|
1578 |
1468 if ( $this->col_info ) { |
1579 if ( $this->col_info ) { |
1469 if ( $col_offset == -1 ) { |
1580 if ( $col_offset == -1 ) { |
1470 $i = 0; |
1581 $i = 0; |
1471 $new_array = array(); |
1582 $new_array = array(); |
1472 foreach( (array) $this->col_info as $col ) { |
1583 foreach( (array) $this->col_info as $col ) { |
1545 * Whether the database supports collation. |
1656 * Whether the database supports collation. |
1546 * |
1657 * |
1547 * Called when WordPress is generating the table scheme. |
1658 * Called when WordPress is generating the table scheme. |
1548 * |
1659 * |
1549 * @since 2.5.0 |
1660 * @since 2.5.0 |
|
1661 * @deprecated 3.5.0 |
|
1662 * @deprecated Use wpdb::has_cap( 'collation' ) |
1550 * |
1663 * |
1551 * @return bool True if collation is supported, false if version does not |
1664 * @return bool True if collation is supported, false if version does not |
1552 */ |
1665 */ |
1553 function supports_collation() { |
1666 function supports_collation() { |
|
1667 _deprecated_function( __FUNCTION__, '3.5', 'wpdb::has_cap( \'collation\' )' ); |
1554 return $this->has_cap( 'collation' ); |
1668 return $this->has_cap( 'collation' ); |
|
1669 } |
|
1670 |
|
1671 /** |
|
1672 * The database character collate. |
|
1673 * |
|
1674 * @since 3.5.0 |
|
1675 * |
|
1676 * @return string The database character collate. |
|
1677 */ |
|
1678 public function get_charset_collate() { |
|
1679 $charset_collate = ''; |
|
1680 |
|
1681 if ( ! empty( $this->charset ) ) |
|
1682 $charset_collate = "DEFAULT CHARACTER SET $this->charset"; |
|
1683 if ( ! empty( $this->collate ) ) |
|
1684 $charset_collate .= " COLLATE $this->collate"; |
|
1685 |
|
1686 return $charset_collate; |
1555 } |
1687 } |
1556 |
1688 |
1557 /** |
1689 /** |
1558 * Determine if a database supports a particular feature |
1690 * Determine if a database supports a particular feature |
1559 * |
1691 * |