# 數據庫驅動器參考
這是一個平臺無關的數據庫實現基類,該類不會被直接調用, 而是通過特定的數據庫適配器類來繼承和實現該類。
關于數據庫驅動器,已經在其他幾篇文檔中介紹過,這篇文檔將作為它們的一個參考。
>[danger] 重要
> 并不是所有的方法都被所有的數據庫驅動器所支持, 當不支持的時候,有些方法可能會失敗(返回 FALSE)。
classCI_DB_driver
>[info] ### initialize()
返回: TRUE on success, FALSE on failure
返回類型: bool
初始化數據庫配置,建立對數據庫的連接。
>[info] ### db_connect($persistent = TRUE)
參數:
* **$persistent**?(bool) -- Whether to establish a persistent connection or a regular one
返回: Database connection resource/object or FALSE on failure
返回類型: mixed
建立對數據庫的連接。
> 注解
> 返回值取決于當前使用的數據庫驅動器,例如?mysqli?實例將會返回 'mysqli' 驅動器。
>[info] ### db_pconnect()
返回: Database connection resource/object or FALSE on failure
返回類型: mixed
建立對數據庫的連接,使用持久連接。
> 注解
> 該方法其實就是調用?db_connect(TRUE)?。
>[info] ### reconnect()
返回: TRUE on success, FALSE on failure
返回類型: bool
如果超過服務器的超時時間都沒有發送任何查詢請求, 使用該方法可以讓數據庫連接保持有效,或重新連接數據庫。
>[info] ### db_select([$database = ''])
參數:
* **$database**?(string) -- Database name
返回: TRUE on success, FALSE on failure
返回類型: bool
切換到某個數據庫。
>[info] ### db_set_charset($charset)
參數:
* **$charset**?(string) -- Character set name
返回: TRUE on success, FALSE on failure
返回類型: bool
設置客戶端字符集。
>[info] ### platform()
返回: Platform name
返回類型: string
當前使用的數據庫平臺(mysql、mssql 等)。
>[info] ### version()
返回: The version of the database being used
返回類型: string
數據庫版本。
>[info] ### query($sql[, $binds = FALSE[, $return_object = NULL]]])
參數:
* **$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
返回: TRUE for successful "write-type" queries, CI_DB_result instance (method chaining) on "query" success, FALSE on failure
返回類型: mixed
執行一個 SQL 查詢。
如果是讀類型的查詢,執行 SQL 成功后將返回結果對象。
有以下幾種可能的返回值:
* 如果是寫類型的查詢,執行成功返回 TRUE
* 執行失敗返回 FALSE
* 如果是讀類型的查詢,執行成功返回?CI_DB_result?對象
>[info] ### simple_query($sql)
參數:
* **$sql**?(string) -- The SQL statement to execute
返回: Whatever the underlying driver's "query" function returns
返回類型: mixed
query()?方法的簡化版,當你只需要簡單的執行一個查詢,并不關心查詢的結果時, 可以使用該方法。
>[info] ### trans_strict([$mode = TRUE])
參數:
* **$mode**?(bool) -- Strict mode flag
返回類型: void
啟用或禁用事務的嚴格模式。
在嚴格模式下,如果你正在運行多組事務,只要有一組失敗,所有組都會被回滾。 如果禁用嚴格模式,那么每一組都被視為獨立的組, 這意味著其中一組失敗不會影響其他的組。
>[info] ### trans_off()
返回類型: void
實時的禁用事務。
>[info] ### trans_start([$test_mode = FALSE])
參數:
* **$test_mode**?(bool) -- Test mode flag
返回類型: void
開啟一個事務。
>[info] ### trans_complete()
返回類型: void
結束事務。
>[info] ### trans_status()
返回: TRUE if the transaction succeeded, FALSE if it failed
返回類型: bool
獲取事務的狀態,用來判斷事務是否執行成功。
>[info] ### compile_binds($sql,?$binds)
參數:
* **$sql**?(string) -- The SQL statement
* **$binds**?(array) -- An array of binding data
返回: The updated SQL statement
返回類型: string
根據綁定的參數值編譯 SQL 查詢。
>[info] ### is_write_type($sql)
參數:
* **$sql**?(string) -- The SQL statement
返回: TRUE if the SQL statement is of "write type", FALSE if not
返回類型: bool
判斷查詢是寫類型(INSERT、UPDATE、DELETE),還是讀類型(SELECT)。
>[info] ### elapsed_time([$decimals = 6])
參數:
* **$decimals**?(int) -- The number of decimal places
返回: The aggregate query elapsed time, in microseconds
返回類型: string
計算查詢所消耗的時間。
>[info] ### total_queries()
返回: The total number of queries executed
返回類型: int
返回當前已經執行了多少次查詢。
>[info] ### last_query()
返回: The last query executed
返回類型: string
返回上一次執行的查詢。
>[info] ### escape($str)
參數:
* **$str**?(mixed) -- The value to escape, or an array of multiple ones
返回: The escaped value(s)
返回類型: mixed
根據輸入數據的類型進行數據轉義,包括布爾值和空值。
>[info] ### escape_str($str[,?$like = FALSE])
參數:
* **$str**?(mixed) -- A string value or array of multiple ones
* **$like**?(bool) -- Whether or not the string will be used in a LIKE condition
返回: The escaped string(s)
返回類型: mixed
轉義字符串。
警告
返回的字符串沒有用引號引起來。
>[info] ### escape_like_str($str)
參數:
* **$str**?(mixed) -- A string value or array of multiple ones
返回: The escaped string(s)
返回類型: mixed
轉義 LIKE 字符串。
和?escape_str()?方法類似,但同時也對 LIKE 語句中的?%?和?_?通配符進行轉義。
>[info] ### primary($table)
參數:
* **$table**?(string) -- Table name
返回: The primary key name, FALSE if none
返回類型: string
獲取一個表的主鍵。
注解
如果數據庫不支持主鍵檢測,將假設第一列就是主鍵。
>[info] ### count_all([$table = ''])
參數:
* **$table**?(string) -- Table name
返回: Row count for the specified table
返回類型: int
返回表中的總記錄數。
>[info] ### list_tables([$constrain_by_prefix = FALSE])
參數:
* **$constrain_by_prefix**?(bool) -- TRUE to match table names by the configured dbprefix
返回: Array of table names or FALSE on failure
返回類型: array
返回當前數據庫的所有表。
>[info] ### table_exists($table_name)
參數:
* **$table_name**?(string) -- The table name
返回: TRUE if that table exists, FALSE if not
返回類型: bool
判斷某個數據庫表是否存在。
>[info] ### list_fields($table)
參數:
* **$table**?(string) -- The table name
返回: Array of field names or FALSE on failure
返回類型: array
返回某個表的所有字段名。
field_exists($field_name,?$table_name)
參數:
* **$table_name**?(string) -- The table name
* **$field_name**?(string) -- The field name
返回:TRUE if that field exists in that table, FALSE if not
返回類型: bool
判斷某個字段是否存在。
>[info] ### field_data($table)
參數:
* **$table**?(string) -- The table name
返回: Array of field data items or FALSE on failure
返回類型: array
獲取某個表的所有字段信息。
>[info] ### escape_identifiers($item)
參數:
* **$item**?(mixed) -- The item or array of items to escape
返回: The input item(s), escaped
返回類型: mixed
對 SQL 標識符進行轉義,例如列名、表名、關鍵字。
>[info] ### insert_string($table,?$data)
參數:
* **$table**?(string) -- The target table
* **$data**?(array) -- An associative array of key/value pairs
返回: The SQL INSERT statement, as a string
返回類型: string
生成 INSERT 語句。
>[info] ### update_string($table,?$data,?$where)
參數:
* **$table**?(string) -- The target table
* **$data**?(array) -- An associative array of key/value pairs
* **$where**?(mixed) -- The WHERE statement conditions
返回: The SQL UPDATE statement, as a string
返回類型: string
生成 UPDATE 語句。
>[info] ### call_function($function)
參數:
* **$function**?(string) -- Function name
返回: The function result
返回類型: string
使用一種平臺無關的方式執行一個原生的 PHP 函數。
>[info] ### cache_set_path([$path = ''])
參數:
* **$path**?(string) -- Path to the cache directory
返回類型: void
設置緩存路徑。
>[info] ### cache_on()
返回: TRUE if caching is on, FALSE if not
返回類型: bool
啟用數據庫結果緩存。
>[info] ### cache_off()
返回: TRUE if caching is on, FALSE if not
|返回類型: bool
禁用數據庫結果緩存。
>[info] ### cache_delete([$segment_one = ''[,?$segment_two = '']])
參數:
* **$segment_one**?(string) -- First URI segment
* **$segment_two**?(string) -- Second URI segment
返回: TRUE on success, FALSE on failure
返回類型: bool
刪除特定 URI 的緩存文件。
>[info] ### cache_delete_all()
返回: TRUE on success, FALSE on failure
返回類型: bool
刪除所有緩存文件。
>[info] ### close()
返回類型: void
關閉數據庫的連接。
>[info] ### display_error([$error = ''[,?$swap = ''[,?$native = FALSE]]])
參數:
* **$error**?(string) -- The error message
* **$swap**?(string) -- Any "swap" values
* **$native**?(bool) -- Whether to localize the message
返回類型: void
返回: Displays the DB error screensends the application/views/errors/error_db.php template
顯示一個錯誤信息,并終止腳本執行。
錯誤信息是使用?application/views/errors/error_db.php?文件中的模板來顯示。
>[info] ### protect_identifiers($item[,?$prefix_single = FALSE[,?$protect_identifiers = NULL[,?$field_exists = TRUE]]])
參數:
* **$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
返回:
The modified item
返回類型: 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?插入到正確的位置。
該方法在查詢構造器類中被廣泛使用。
- 歡迎使用 CodeIgniter
- 安裝說明
- 下載 CodeIgniter
- 安裝說明
- 從老版本升級
- 疑難解答
- CodeIgniter 概覽
- CodeIgniter 將從這里開始
- CodeIgniter 是什么?
- 支持特性
- 應用程序流程圖
- 模型-視圖-控制器
- 設計與架構目標
- 教程 - 內容提要
- 加載靜態內容
- 讀取新聞條目
- 創建新聞條目
- 結束語
- 常規主題
- CodeIgniter URL
- 控制器
- 保留名稱
- 視圖
- 模型
- 輔助函數
- 使用 CodeIgniter 類庫
- 創建類庫
- 使用 CodeIgniter 驅動器
- 創建驅動器
- 創建核心系統類
- 創建附屬類
- 鉤子 - 擴展框架核心
- 自動加載資源
- 公共函數
- 兼容性函數
- URI 路由
- 錯誤處理
- 網頁緩存
- 程序分析
- 以 CLI 方式運行
- 管理你的應用程序
- 處理多環境
- 在視圖文件中使用 PHP 替代語法
- 安全
- PHP 開發規范
- 類庫參考
- 基準測試類
- 緩存驅動器
- 日歷類
- 購物車類
- 配置類
- Email 類
- 加密類
- 加密類(新版)
- 文件上傳類
- 表單驗證類
- FTP 類
- 圖像處理類
- 輸入類
- Javascript 類
- 語言類
- 加載器類
- 遷移類
- 輸出類
- 分頁類
- 模板解析類
- 安全類
- Session 類
- HTML 表格類
- 引用通告類
- 排版類
- 單元測試類
- URI 類
- 用戶代理類
- XML-RPC 與 XML-RPC 服務器類
- Zip 編碼類
- 數據庫參考
- 數據庫快速入門: 示例代碼
- 數據庫配置
- 連接你的數據庫
- 查詢
- 生成查詢結果
- 查詢輔助函數
- 查詢構造器類
- 事務
- 數據庫元數據
- 自定義函數調用
- 數據庫緩存類
- 數據庫工廠類
- 數據庫工具類
- 數據庫驅動器參考
- 輔助函數參考
- 數組輔助函數
- 驗證碼輔助函數
- Cookie 輔助函數
- 日期輔助函數
- 目錄輔助函數
- 下載輔助函數
- 郵件輔助函數
- 文件輔助函數
- 表單輔助函數
- HTML 輔助函數
- 語言輔助函數
- Inflector 輔助函數
- 數字輔助函數
- 路徑輔助函數
- 安全輔助函數
- 表情輔助函數
- 字符串輔助函數
- 文本輔助函數
- 排版輔助函數
- URL 輔助函數
- XML 輔助函數
- 向 CodeIgniter 貢獻你的力量