<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 生成查詢結果 有幾種不同方法可以生成查詢結果: [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.
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看