# 生成查詢結果
有幾種不同方法可以生成查詢結果:
[TOC=2,3]
## 結果數組
**result()**?方法
該方法以**對象數組**形式返回查詢結果,如果查詢失敗返回**空數組**。 一般情況下,你會像下面這樣在一個 foreach 循環中使用它:
~~~
$query = $this->db->query("YOUR QUERY");
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
~~~
該方法是?result_object()?方法的別名。
如果你的查詢可能會沒有結果,推薦在處理結果之前,先使用方法?num_rows()?檢驗一下:
~~~
$query = $this->db->query("YOUR QUERY");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
}
~~~
你還可以傳一個字符串參數給?result()?方法,這個字符串參數代表 你想要把每個結果轉換成某個類的類名(這個類必須已經加載)
~~~
$query = $this->db->query("SELECT * FROM users;");
foreach ($query->result('User') as $user)
{
echo $user->name; // access attributes
echo $user->reverse_name(); // or methods defined on the 'User' class
}
~~~
**result_array()**?方法
這個方法以**一個純粹的數組**形式返回查詢結果,如果無結果,則返回一個空數組。 一般情況下,你會像下面這樣在一個 foreach 循環中使用它:
~~~
$query = $this->db->query("YOUR QUERY");
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
~~~
## 結果行
**row()**?方法
這個方法返回單獨一行結果。如果你的查詢不止一行結果,它只返回第一行。 返回的結果是**對象**形式,這里是用例:
~~~
$query = $this->db->query("YOUR QUERY");
if ($query->num_rows() > 0)
{
$row = $query->row();
echo $row->title;
echo $row->name;
echo $row->body;
}
~~~
如果你要返回特定行的數據,你可以將行號作為第一個參數傳給這個方法:
~~~
$row = $query->row(5);
~~~
你還可以加上第二個參數,該參數為字符串類型,代表你想要把結果轉換成某個類的類名:
~~~
$query = $this->db->query("SELECT * FROM users LIMIT 1;");
$row = $query->row(0, 'User');
echo $row->name; // access attributes
echo $row->reverse_name(); // or methods defined on the 'User' class
~~~
**row_array()**?方法
這個方法除了返回結果是一個數組而不是一個對象之外,其他的和上面的?row()?方法完全一樣。 舉例:
~~~
$query = $this->db->query("YOUR QUERY");
if ($query->num_rows() > 0)
{
$row = $query->row_array();
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
~~~
如果你要返回特定行的數據,你可以將行號作為第一個參數傳給這個方法:
~~~
$row = $query->row_array(5);
~~~
另外,你可以使用下面這些方法從你的結果集中獲取前一個、后一個、 第一個或者最后一個結果:
>
>
>
>
> **$row = $query->first_row()**
>
> **$row = $query->last_row()**
>
> **$row = $query->next_row()**
>
> **$row = $query->previous_row()**
>
>
>
>
這些方法默認返回對象,如果需要返回數組形式,將單詞 "array" 作為參數傳入方法即可:
>
>
>
>
> **$row = $query->first_row('array')**
>
> **$row = $query->last_row('array')**
>
> **$row = $query->next_row('array')**
>
> **$row = $query->previous_row('array')**
>
>
>
>
注解
上面所有的這些方法都會把所有的結果加載到內存里(預讀取), 當處理大結果集時最好使用?unbuffered_row()?方法。
**unbuffered_row()**?方法
這個方法和?row()?方法一樣返回單獨一行結果,但是它不會預讀取所有的結果數據到內存中。 如果你的查詢結果不止一行,它將返回當前一行,并通過內部實現的指針來移動到下一行。
~~~
$query = $this->db->query("YOUR QUERY");
while ($row = $query->unbuffered_row())
{
echo $row->title;
echo $row->name;
echo $row->body;
}
~~~
為了指定返回值的類型,可以傳一個字符串參數 'object'(默認值) 或者 'array' 給這個方法:
~~~
$query->unbuffered_row(); // object
$query->unbuffered_row('object'); // object
$query->unbuffered_row('array'); // associative array
~~~
## 結果輔助方法
**num_rows()**?方法
該方法返回查詢結果的行數。注意:在這個例子中,$query?變量為查詢結果對象:
~~~
$query = $this->db->query('SELECT * FROM my_table');
echo $query->num_rows();
~~~
注解
并不是所有的數據庫驅動器都有原生的方法來獲取查詢結果的總行數。 當遇到這種情況時,所有的數據會被預讀取到內存中,并調用?count()?函數 來取得總行數。
**num_fields()**?方法
該方法返回查詢結果的字段數(列數)。在你的查詢結果對象上調用該方法:
~~~
$query = $this->db->query('SELECT * FROM my_table');
echo $query->num_fields();
~~~
**free_result()**?方法
該方法釋放掉查詢結果所占的內存,并刪除結果的資源標識。通常來說, PHP 會在腳本執行結束后自動釋放內存。但是,如果你在某個腳本中執行大量的查詢, 你可能需要在每次查詢之后釋放掉查詢結果,以此來降低內存消耗。
舉例:
~~~
$query = $this->db->query('SELECT title FROM my_table');
foreach ($query->result() as $row)
{
echo $row->title;
}
$query->free_result(); // The $query result object will no longer be available
$query2 = $this->db->query('SELECT name FROM some_table');
$row = $query2->row();
echo $row->name;
$query2->free_result(); // The $query2 result object will no longer be available
~~~
**data_seek()**?方法
這個方法用來設置下一個結果行的內部指針,它只有在和?unbuffered_row()?方法一起使用才有效果。
它接受一個正整數參數(默認值為0)表示想要讀取的下一行,返回值為 TRUE 或 FALSE 表示成功或失敗。
~~~
$query = $this->db->query('SELECT `field_name` FROM `table_name`');
$query->data_seek(5); // Skip the first 5 rows
$row = $query->unbuffered_row();
~~~
注解
并不是所有的數據庫驅動器都支持這一特性,調用這個方法將會返回 FALSE, 譬如你無法在 PDO 上使用它。
## 類參考
classCI_DB_result
result([$type = 'object'])
參數:
* **$type**?(string) -- Type of requested results - array, object, or class name
返回: Array containing the fetched rows
返回類型: array
A wrapper for the?result_array(),?result_object()?and?custom_result_object()?methods.
Usage: see?[結果數組](http://codeigniter.org.cn/user_guide/database/results.html#id2).
result_array()
返回: Array containing the fetched rows
返回類型: array
Returns the query results as an array of rows, where each row is itself an associative array.
Usage: see?[結果數組](http://codeigniter.org.cn/user_guide/database/results.html#id2).
result_object()
返回: Array containing the fetched rows
返回類型: array
Returns the query results as an array of rows, where each row is an object of type?stdClass.
Usage: see?[結果數組](http://codeigniter.org.cn/user_guide/database/results.html#id2).
custom_result_object($class_name)
參數:
* **$class_name**?(string) -- Class name for the resulting rows
返回: Array containing the fetched rows
返回類型: array
Returns the query results as an array of rows, where each row is an instance of the specified class.
row([$n = 0[,?$type = 'object']])
參數:
* **$n**?(int) -- Index of the query results row to be returned
* **$type**?(string) -- Type of the requested result - array, object, or class name
返回: The requested row or NULL if it doesn't exist
返回類型: mixed
A wrapper for the?row_array(),?row_object()?and?``custom_row_object()?methods.
Usage: see?[結果行](http://codeigniter.org.cn/user_guide/database/results.html#id3).
unbuffered_row([$type = 'object'])
參數:
* **$type**?(string) -- Type of the requested result - array, object, or class name
返回: Next row from the result set or NULL if it doesn't exist
返回類型: mixed
Fetches the next result row and returns it in the requested form.
Usage: see?[結果行](http://codeigniter.org.cn/user_guide/database/results.html#id3).
row_array([$n = 0])
參數:
* **$n**?(int) -- Index of the query results row to be returned
返回: The requested row or NULL if it doesn't exist
返回類型: array
Returns the requested result row as an associative array.
Usage: see?[結果行](http://codeigniter.org.cn/user_guide/database/results.html#id3).
row_object([$n = 0])
參數:
* **$n**?(int) -- Index of the query results row to be returned :returns: The requested row or NULL if it doesn't exist
返回類型: stdClass
Returns the requested result row as an object of type?stdClass.
Usage: see?[結果行](http://codeigniter.org.cn/user_guide/database/results.html#id3).
custom_row_object($n,?$type)
參數:
* **$n**?(int) -- Index of the results row to return
* **$class_name**?(string) -- Class name for the resulting row
返回: The requested row or NULL if it doesn't exist
返回類型: $type
Returns the requested result row as an instance of the requested class.
data_seek([$n = 0])
參數:
* **$n**?(int) -- Index of the results row to be returned next
返回: TRUE on success, FALSE on failure
返回類型: bool
Moves the internal results row pointer to the desired offset.
Usage: see?[結果輔助方法](http://codeigniter.org.cn/user_guide/database/results.html#id4).
set_row($key[,?$value = NULL])
參數:
* **$key**?(mixed) -- Column name or array of key/value pairs
* **$value**?(mixed) -- Value to assign to the column, $key is a single field name
返回類型: void
Assigns a value to a particular column.
next_row([$type = 'object'])
參數:
* **$type**?(string) -- Type of the requested result - array, object, or class name
返回: Next row of result set, or NULL if it doesn't exist
返回類型: mixed
Returns the next row from the result set.
previous_row([$type = 'object'])
參數:
* **$type**?(string) -- Type of the requested result - array, object, or class name
返回: Previous row of result set, or NULL if it doesn't exist
返回類型: mixed
Returns the previous row from the result set.
first_row([$type = 'object'])
參數:
* **$type**?(string) -- Type of the requested result - array, object, or class name
返回: First row of result set, or NULL if it doesn't exist
返回類型: mixed
Returns the first row from the result set.
last_row([$type = 'object'])
參數:
* **$type**?(string) -- Type of the requested result - array, object, or class name
返回: Last row of result set, or NULL if it doesn't exist
返回類型: mixed
Returns the last row from the result set.
num_rows()
返回: Number of rows in the result set
返回類型: int
Returns the number of rows in the result set.
Usage: see?[結果輔助方法](http://codeigniter.org.cn/user_guide/database/results.html#id4).
num_fields()
返回: Number of fields in the result set
返回類型: int
Returns the number of fields in the result set.
Usage: see?[結果輔助方法](http://codeigniter.org.cn/user_guide/database/results.html#id4).
field_data()
返回: Array containing field meta-data
返回類型: array
Generates an array of?stdClass?objects containing field meta-data.
free_result()
返回類型: void
Frees a result set.
Usage: see?[結果輔助方法](http://codeigniter.org.cn/user_guide/database/results.html#id4).
list_fields()
返回: Array of column names
返回類型: array
Returns an array containing the field names in the result set.
- 歡迎使用 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 貢獻你的力量