簡體   English   中英

如何在不使用codeigniter中的result()或row()的情況下獲取查詢結果

[英]how to get result of query without using result() or row() in codeigniter

我在名為get_all_Emp()模型中返回一個函數,

return $this->db->get();

我在控制器中稱呼為

$items    = $this->Item->get_all_Emp();

但是返回而不是返回結果,而是返回CI_DB_mysqli_result Object即,

 CI_DB_mysqli_result Object
(
    [conn_id] => mysqli Object
        (
            [affected_rows] => 24
            [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $
            [client_version] => 50011
            [connect_errno] => 0
            [connect_error] => 
            [errno] => 0
            [error] => 
            [error_list] => Array
                (
                )

            [field_count] => 44
            [host_info] => Localhost via UNIX socket
            [info] => 
            [insert_id] => 0
            [server_info] => 5.7.22-0ubuntu0.16.04.1
            [server_version] => 50722
            [stat] => Uptime: 540  Threads: 1  Questions: 1117  Slow queries: 0  Opens: 385  Flush tables: 1  Open tables: 171  Queries per second avg: 2.068
            [sqlstate] => 00000
            [protocol_version] => 10
            [thread_id] => 36
            [warning_count] => 0
        )

    [result_id] => mysqli_result Object
        (
            [current_field] => 0
            [field_count] => 44
            [lengths] => 
            [num_rows] => 24
            [type] => 0
        )

    [result_array] => Array
        (
        )

    [result_object] => Array
        (
        )

    [custom_result_object] => Array
        (
        )

    [current_row] => 0
    [num_rows] => 24
    [row_data] => 
)

但是如果我使用,

$query = $this->db->get();
        $result = $query->result();
        return $result;

而不是return get_all_Emp()我得到了適當的結果。 所以有什么辦法可以使用以前的方法做到這一點,並且仍然得到答案謝謝您的建議

對於數組結果,可以使用result_array()。 只需在您的模型中返回:

return $this->db->get("table_name")->result_array();

得到這樣的東西之后

Array(
    [0] => Array('col1' => 'somedata', 'col2' => 'somedata2'),
    [1] => Array('col1' => 'somedata', 'col2' => 'somedata2'),
)

這里的文件

您似乎在數組和對象之間感到困惑。 通過簡單的Google搜索,有很多與此相關的信息,但從本質上講,它可以歸結為:

如果您有一列名為name的數組,則您可以執行類似$row['name'] ,而使用對象則可以執行$row->name則表示法略有不同,其風格更多。 我更喜歡使用一個對象,但是如果必須進行操作,數組會更有用。

進一步的get()實際上並不返回結果對象。

  1. $this->db->get()->result()返回參數內所有行的對象
  2. $this->db->get()->row()返回$this->db->get()->row()對象
  3. $this->db->get()->result_array()返回參數中所有行的數組
  4. $this->db->get()->row_array()返回單個行的數組

因此,在函數中,如果要使用數組,請return $this->db-get()->result_array(); 假設您已經設置了起始條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM