Blog

wordpress数据库操作类wpdb相关注释说明

以下相关代码注释可能不全面,有不对的地方,仅做参考; show_errors();// 是否显示sql/DB的错误开关 默认是false 关闭的

  $this->init_charset();

  $this->dbuser = $dbuser;
  $this->dbpassword = $dbpassword; $this->dbname = $dbname; $this->dbhost = $dbhost; $this->db_connect(); } //类析构函数 function __destruct() { return true; } //设置编码变量 function init_charset() { if ( function_exists('is_multisite') && is_multisite() ) { $this->charset = 'utf8'; if ( defined( 'DB_COLLATE' ) && DB_COLLATE ) $this->collate = DB_COLLATE; else $this->collate = 'utf8_general_ci'; } elseif ( defined( 'DB_COLLATE' ) ) { $this->collate = DB_COLLATE; } if ( defined( 'DB_CHARSET' ) ) $this->charset = DB_CHARSET; } // 设置数据库查询编码 function set_charset($dbh, $charset = null, $collate = null) { if ( !isset($charset) ) $charset = $this->charset; if ( !isset($collate) ) $collate = $this->collate; if ( $this->has_cap( 'collation', $dbh ) && !empty( $charset ) ) { if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset', $dbh ) ) { mysql_set_charset( $charset, $dbh ); $this->real_escape = true; } else { $query = $this->prepare( 'SET NAMES %s', $charset ); if ( ! empty( $collate ) ) $query .= $this->prepare( ' COLLATE %s', $collate ); mysql_query( $query, $dbh ); } } } // 设置表前缀 function set_prefix( $prefix, $set_table_names = true ) { .......... } //设置博客id function set_blog_id( $blog_id, $site_id = 0 ) { ............... } //获取博客前缀 function get_blog_prefix( $blog_id = null ) { if ( is_multisite() ) { if ( null === $blog_id ) $blog_id = $this->blogid; $blog_id = (int) $blog_id; if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) ) return $this->base_prefix; else return $this->base_prefix . $blog_id . '_'; } else { return $this->base_prefix; } } //返回一个数组的Word Press表 function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) { ............... } //选择数据库 function select( $db, $dbh = null) { if ( is_null($dbh) ) $dbh = $this->dbh; if ( !@mysql_select_db( $db, $dbh ) ) { $this->ready = false; $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/'

无法选择数据库

我们可以和数据库服务器建立连接(说明您提供的用户名和密码正确),但无法选择(select) %1$s 数据库。
  • 您确认数据库存在?
  • 您确认用户 %2$s 拥有使用 %1$s 数据库的权限?
  • 在某些系统上您的数据库名可能还包含了您用户名的前缀,例如 username_%1$s,问题会不会出在这里?
如果您不知道如何设置一个数据库,您应该联系您的主机管理员。如果所有办法还没有解决您的问题,您可以在 http://zh-cn.forums.wordpress.org/">WordPress 中 文论坛上寻求帮助。 '/*/WP_I18N_DB_SELECT_DB*/, $db, $this->dbuser ), 'db_select_fail' ); return; } } //对特殊字符转义 function _weak_escape( $string ) { return addslashes( $string ); } //判断$this->real_escape为true还是false,true就用数据库转义mysql_real_escape_string(),否则用addslashes /*mysql_real_escape_string 优于addslashes mysql_real_escape_string考虑到字符集的问题 mysql_real_escape_string最好与mysql_set_charset一起使用因为都涉及字符集的问题,mysql_set_charset而是设字符集的 */ function _real_escape( $string ) { if ( $this->dbh && $this->real_escape ) return mysql_real_escape_string( $string, $this->dbh ); else return addslashes( $string ); } //字符串数组转义 function _escape( $data ) { if ( is_array( $data ) ) { foreach ( (array) $data as $k => $v ) { if ( is_array($v) ) $data[$k] = $this->_escape( $v ); else $data[$k] = $this->_real_escape( $v ); } } else { $data = $this->_real_escape( $data ); } return $data; } //内容转义(是否为数组) function escape( $data ) { if ( is_array( $data ) ) { foreach ( (array) $data as $k => $v ) { if ( is_array( $v ) ) $data[$k] = $this->escape( $v ); else $data[$k] = $this->_weak_escape( $v ); } } else { $data = $this->_weak_escape( $data ); } return $data; } function escape_by_ref( &$string ) { $string = $this->_real_escape( $string ); } //数据执行代码安全保障 function prepare( $query = null ) { // ( $query, *$args ) if ( is_null( $query ) ) return; $args = func_get_args(); array_shift( $args ); // If args were passed as an array (as in vsprintf), move them up if ( isset( $args[0] ) && is_array($args[0]) ) $args = $args[0]; $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s   array_walk( $args, array( &$this, 'escape_by_ref' ) );   return @vsprintf( $query, $args );  }   //打印sql错误信息  function print_error( $str = '' ) {   ............  }   //设置显示错误信息  function show_errors( $show = true ) {   $errors = $this->show_errors; $this->show_errors = $show; return $errors; } //设置隐藏错误信息 function hide_errors() { $show = $this->show_errors; $this->show_errors = false; return $show; } //是否抑制错误信息 function suppress_errors( $suppress = true ) { $errors = $this->suppress_errors; $this->suppress_errors = (bool) $suppress; return $errors; } function flush() { $this->last_result = array(); $this->col_info    = null; $this->last_query  = null; } //数据库连接 function db_connect() { ......... } //数据库执行语句 function query( $query ) { $return_val     = $num_rows; } return $return_val; } //数据库插入 function insert( $table, $data, $format = null ) { return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' ); } //数据替换 function replace( $table, $data, $format = null ) { return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' ); } //数据插入有insert into和replace into两种 function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) { if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) return false; $formats = $format = (array) $format; $fields = array_keys( $data ); $formatted_fields = array(); foreach ( $fields as $field ) { if ( !empty( $format ) ) $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; elseif ( isset( $this->field_types[$field] ) ) $form = $this->field_types[$field]; else $form = '%s'; $formatted_fields[] = $form; } $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')"; return $this->query( $this->prepare( $sql, $data ) ); } //数据库更新 function update( $table, $data, $where, $format = null, $where_format = null ) { ............. } //取得执行语句的相关数组信息 function get_var( $query = null, $x = 0, $y = 0 ) { $this->func_call = "$db->get_var("$query", $x, $y)"; if ( $query ) $this->query( $query ); // Extract var out of cached results based x,y vals if ( !empty( $this->last_result[$y] ) ) { $values = array_values( get_object_vars( $this->last_result[$y] ) ); } // If there is a value return it else return null return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null; } //取得一行数据信息 function get_row( $query = null, $output = OBJECT, $y = 0 ) { $this->func_call = "$db->get_row("$query",$output,$y)"; if ( $query ) $this->query( $query ); else return null; if ( !isset( $this->last_result[$y] ) ) return null; if ( $output == OBJECT ) { return $this->last_result[$y] ? $this->last_result[$y] : null; } elseif ( $output == ARRAY_A ) { return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null; } elseif ( $output == ARRAY_N ) { return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null; } else { $this->print_error(/*WP_I18N_DB_GETROW_ERROR*/' $db->get_row(string query, output type, int offset) —— 输出类型需为 OBJECT、ARRAY_A,或 ARRAY_N'/*/WP_I18N_DB_GETROW_ERROR*/); } } //获得一列数据执行结果 function get_col( $query = null , $x = 0 ) { if ( $query ) $this->query( $query ); $new_array = array(); // Extract the column values for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {    $new_array[$i] = $this->get_var( null, $x, $i ); } return $new_array; } //返回整个执行结果 function get_results( $query = null, $output = OBJECT ) { .......... } //取得最后一次结果集中的一列信息 function get_col_info( $info_type = 'name', $col_offset = -1 ) { ................ } //计时器开始 function timer_start() { $mtime            = explode( ' ', microtime() ); $this->time_start = $mtime[1] + $mtime[0]; return true; } //计时结束返回总共时间 function timer_stop() { $mtime      = explode( ' ', microtime() ); $time_end   = $mtime[1] + $mtime[0]; $time_total = $time_end - $this->time_start; return $time_total; } //在头部和尾部中包含错误信息 function bail( $message, $error_code = '500' ) { if ( !$this->show_errors ) { if ( class_exists( 'WP_Error' ) ) $this->error = new WP_Error($error_code, $message); else $this->error = $message; return false; } wp_die($message); } //数据库版本检查 function check_database_version() { global $wp_version, $required_mysql_version; // Make sure the server has the required MySQL version if ( version_compare($this->db_version(), $required_mysql_version, '<') ) return new WP_Error('database_version', sprintf( __( 'ERROR: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version )); } //是否数据库需要升级 function supports_collation() { return $this->has_cap( 'collation' ); } //确定一个数据库支持特定功能 function has_cap( $db_cap ) { $version = $this->db_version(); switch ( strtolower( $db_cap ) ) { case 'collation' :    // @since 2.5.0 case 'group_concat' : // @since 2.7 case 'subqueries' :   // @since 2.7 return version_compare( $version, '4.1', '>=' ); case 'set_charset' : return version_compare($version, '5.0.7', '>='); }; return false; } //返回调用函数的名称字符串 function get_caller() { $trace  = array_reverse( debug_backtrace() ); $caller = array(); foreach ( $trace as $call ) { if ( isset( $call['class'] ) && __CLASS__ == $call['class'] ) continue; // Filter out wpdb calls. $caller[] = isset( $call['class'] ) ? "{$call['class']}->{$call['function']}" : $call['function']; } return join( ', ', $caller ); } //取得数据库版本信息 function db_version() { return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); } } ?>

Words translated by CCJK

146,096,379

Request a call or contact us

  •  

    Call ccjk.support for professional assistance

  •  

    Click Here to leave us a message and be as detailed as possible.

We are Certified

Our Client Satisfaction

rating for previous quarte

4.00
  •  
    Inquiry Form

    Please take few minutes to fill
    inquiry form and get Free Quote

  •  
    Request a Call

    Click to receive Free Call, Our
    Representative will contact you

  •  
    Leave a Message

    Please leave us a message and
    be as detailed as possible

Over 95% of our clients recommend our language services to others


Copyright © CCJK Technologies Co., Ltd. 2000-2017. All rights reserved.
TOP