数据库驱动器参考

这是一个平台无关的数据库实现基类,该类不会被直接调用, 而是通过特定的数据库适配器类来继承和实现该类。

关于数据库驱动器,已经在其他几篇文档中介绍过,这篇文档将作为它们的一个参考。

Important

并不是所有的方法都被所有的数据库驱动器所支持, 当不支持的时候,有些方法可能会失败(返回 FALSE)。

class CI_DB_driver
initialize()
Returns:TRUE on success, FALSE on failure
Return type:bool

初始化数据库配置,建立对数据库的连接。

db_connect($persistent = TRUE)
Parameters:
  • $persistent (bool) – Whether to establish a persistent connection or a regular one
Returns:

Database connection resource/object or FALSE on failure

Return type:

mixed

建立对数据库的连接。

Note

返回值取决于当前使用的数据库驱动器,例如 mysqli 实例将会返回 ‘mysqli’ 驱动器。

db_pconnect()
Returns:Database connection resource/object or FALSE on failure
Return type:mixed

建立对数据库的连接,使用持久连接。

Note

该方法其实就是调用 db_connect(TRUE)

reconnect()
Returns:TRUE on success, FALSE on failure
Return type:bool

如果超过服务器的超时时间都没有发送任何查询请求, 使用该方法可以让数据库连接保持有效,或重新连接数据库。

db_select([$database = ''])
Parameters:
  • $database (string) – Database name
Returns:

TRUE on success, FALSE on failure

Return type:

bool

切换到某个数据库。

db_set_charset($charset)
Parameters:
  • $charset (string) – Character set name
Returns:

TRUE on success, FALSE on failure

Return type:

bool

设置客户端字符集。

platform()
Returns:Platform name
Return type:string

当前使用的数据库平台(mysql、mssql 等)。

version()
Returns:The version of the database being used
Return type:string

数据库版本。

query($sql[, $binds = FALSE[, $return_object = NULL]]])
Parameters:
  • $sql (string) – The SQL statement to execute
  • $binds (array) – An array of binding data
  • $return_object (bool) – Whether to return a result object or not
Returns:

TRUE for successful “write-type” queries, CI_DB_result instance (method chaining) on “query” success, FALSE on failure

Return type:

mixed

执行一个 SQL 查询。

如果是读类型的查询,执行 SQL 成功后将返回结果对象。

有以下几种可能的返回值:

  • 如果是写类型的查询,执行成功返回 TRUE
  • 执行失败返回 FALSE
  • 如果是读类型的查询,执行成功返回 CI_DB_result 对象
simple_query($sql)
Parameters:
  • $sql (string) – The SQL statement to execute
Returns:

Whatever the underlying driver’s “query” function returns

Return type:

mixed

query() 方法的简化版,当你只需要简单的执行一个查询,并不关心查询的结果时, 可以使用该方法。

trans_strict([$mode = TRUE])
Parameters:
  • $mode (bool) – Strict mode flag
Return type:

void

启用或禁用事务的严格模式。

在严格模式下,如果你正在运行多组事务,只要有一组失败,所有组都会被回滚。 如果禁用严格模式,那么每一组都被视为独立的组, 这意味着其中一组失败不会影响其他的组。

trans_off()
Return type:void

实时的禁用事务。

trans_start([$test_mode = FALSE])
Parameters:
  • $test_mode (bool) – Test mode flag
Return type:

void

开启一个事务。

trans_complete()
Return type:void

结束事务。

trans_status()
Returns:TRUE if the transaction succeeded, FALSE if it failed
Return type:bool

获取事务的状态,用来判断事务是否执行成功。

compile_binds($sql, $binds)
Parameters:
  • $sql (string) – The SQL statement
  • $binds (array) – An array of binding data
Returns:

The updated SQL statement

Return type:

string

根据绑定的参数值编译 SQL 查询。

is_write_type($sql)
Parameters:
  • $sql (string) – The SQL statement
Returns:

TRUE if the SQL statement is of “write type”, FALSE if not

Return type:

bool

判断查询是写类型(INSERT、UPDATE、DELETE),还是读类型(SELECT)。

elapsed_time([$decimals = 6])
Parameters:
  • $decimals (int) – The number of decimal places
Returns:

The aggregate query elapsed time, in microseconds

Return type:

string

计算查询所消耗的时间。

total_queries()
Returns:The total number of queries executed
Return type:int

返回当前已经执行了多少次查询。

last_query()
Returns:The last query executed
Return type:string

返回上一次执行的查询。

escape($str)
Parameters:
  • $str (mixed) – The value to escape, or an array of multiple ones
Returns:

The escaped value(s)

Return type:

mixed

根据输入数据的类型进行数据转义,包括布尔值和空值。

escape_str($str[, $like = FALSE])
Parameters:
  • $str (mixed) – A string value or array of multiple ones
  • $like (bool) – Whether or not the string will be used in a LIKE condition
Returns:

The escaped string(s)

Return type:

mixed

转义字符串。

Warning

返回的字符串没有用引号引起来。

escape_like_str($str)
Parameters:
  • $str (mixed) – A string value or array of multiple ones
Returns:

The escaped string(s)

Return type:

mixed

转义 LIKE 字符串。

escape_str() 方法类似,但同时也对 LIKE 语句中的 %_ 通配符进行转义。

primary($table)
Parameters:
  • $table (string) – Table name
Returns:

The primary key name, FALSE if none

Return type:

string

获取一个表的主键。

Note

如果数据库不支持主键检测,将假设第一列就是主键。

count_all([$table = ''])
Parameters:
  • $table (string) – Table name
Returns:

Row count for the specified table

Return type:

int

返回表中的总记录数。

list_tables([$constrain_by_prefix = FALSE])
Parameters:
  • $constrain_by_prefix (bool) – TRUE to match table names by the configured dbprefix
Returns:

Array of table names or FALSE on failure

Return type:

array

返回当前数据库的所有表。

table_exists($table_name)
Parameters:
  • $table_name (string) – The table name
Returns:

TRUE if that table exists, FALSE if not

Return type:

bool

判断某个数据库表是否存在。

list_fields($table)
Parameters:
  • $table (string) – The table name
Returns:

Array of field names or FALSE on failure

Return type:

array

返回某个表的所有字段名。

field_exists($field_name, $table_name)
Parameters:
  • $table_name (string) – The table name
  • $field_name (string) – The field name
Returns:

TRUE if that field exists in that table, FALSE if not

Return type:

bool

判断某个字段是否存在。

field_data($table)
Parameters:
  • $table (string) – The table name
Returns:

Array of field data items or FALSE on failure

Return type:

array

获取某个表的所有字段信息。

escape_identifiers($item)
Parameters:
  • $item (mixed) – The item or array of items to escape
Returns:

The input item(s), escaped

Return type:

mixed

对 SQL 标识符进行转义,例如列名、表名、关键字。

insert_string($table, $data)
Parameters:
  • $table (string) – The target table
  • $data (array) – An associative array of key/value pairs
Returns:

The SQL INSERT statement, as a string

Return type:

string

生成 INSERT 语句。

update_string($table, $data, $where)
Parameters:
  • $table (string) – The target table
  • $data (array) – An associative array of key/value pairs
  • $where (mixed) – The WHERE statement conditions
Returns:

The SQL UPDATE statement, as a string

Return type:

string

生成 UPDATE 语句。

call_function($function)
Parameters:
  • $function (string) – Function name
Returns:

The function result

Return type:

string

使用一种平台无关的方式执行一个原生的 PHP 函数。

cache_set_path([$path = ''])
Parameters:
  • $path (string) – Path to the cache directory
Return type:

void

设置缓存路径。

cache_on()
Returns:TRUE if caching is on, FALSE if not
Return type:bool

启用数据库结果缓存。

cache_off()
Returns:TRUE if caching is on, FALSE if not
Return type:bool

禁用数据库结果缓存。

cache_delete([$segment_one = ''[, $segment_two = '']])
Parameters:
  • $segment_one (string) – First URI segment
  • $segment_two (string) – Second URI segment
Returns:

TRUE on success, FALSE on failure

Return type:

bool

删除特定 URI 的缓存文件。

cache_delete_all()
Returns:TRUE on success, FALSE on failure
Return type:bool

删除所有缓存文件。

close()
Return type:void

关闭数据库的连接。

display_error([$error = ''[, $swap = ''[, $native = FALSE]]])
Parameters:
  • $error (string) – The error message
  • $swap (string) – Any “swap” values
  • $native (bool) – Whether to localize the message
Return type:

void

Returns:

Displays the DB error screensends the application/views/errors/error_db.php template

显示一个错误信息,并终止脚本执行。

错误信息是使用 application/views/errors/error_db.php 文件中的模板来显示。

protect_identifiers($item[, $prefix_single = FALSE[, $protect_identifiers = NULL[, $field_exists = TRUE]]])
Parameters:
  • $item (string) – The item to work with
  • $prefix_single (bool) – Whether to apply the dbprefix even if the input item is a single identifier
  • $protect_identifiers (bool) – Whether to quote identifiers
  • $field_exists (bool) – Whether the supplied item contains a field name or not
Returns:

The modified item

Return type:

string

根据配置的 dbprefix 参数,给列名或表名(可能是表别名)添加一个前缀。

为了处理包含路径的列名,必须要考虑一些逻辑。

例如下面的查询:

SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table

或者下面这个查询,使用了表别名:

SELECT m.member_id, m.member_name FROM members AS m

由于列名可以包含四段(主机、数据库名、表名、字段名)或者有一个表别名的前缀, 我们需要做点工作来判断这一点,才能将 dbprefix 插入到正确的位置。

该方法在查询构造器类中被广泛使用。