【问题标题】:how to get result of query without using result() or row() in codeigniter如何在codeigniter中不使用result()或row()获取查询结果
【发布时间】:2018-07-13 06:12:00
【问题描述】:

我有一个名为 get_all_Emp() 的模型返回函数,

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

我在控制器中调用它,

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

但它不是返回结果而是返回CI_DB_mysqli_result Objectie.,

 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() 我得到了适当的结果。 那么有没有办法使用以前的方法来做它并且仍然得到答案 谢谢你的建议

【问题讨论】:

  • 在你的控制器中使用$items = $this->Item->get_all_Emp()->result();
  • 然后我得到“不能使用 stdClass 类型的对象作为数组”
  • 因为它不是一个数组。它是一个对象

标签: php sql database codeigniter


【解决方案1】:

您似乎对数组和对象感到困惑。通过简单的谷歌搜索可以找到大量关于此的信息,但基本上可以归结为:

如果您有一个名为name 的列,其中包含一个数组,您可以执行$row['name'] 之类的操作,而您可以执行$row->name 之类的操作,则表示法有点不同,它更多的是一种风格问题。我更喜欢使用对象,但如果我必须进行操作,数组会更有用。

Further get() 实际上并没有返回结果对象。

  1. $this->db->get()->result() 为您的参数中的所有行返回一个对象
  2. $this->db->get()->row() 为单行返回一个对象
  3. $this->db->get()->result_array() 为您的参数中的所有行返回一个数组
  4. $this->db->get()->row_array() 为单行返回一个数组

所以在你的函数中,如果你想使用一个数组,假设你已经设置了一个来自条件的return $this->db-get()->result_array();

【讨论】:

    【解决方案2】:

    对于数组结果,您可以使用 result_array()。只需在你的模型中返回这个:

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

    在你得到这样的东西之后

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

    文档here

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多