简体   繁体   English

如何从Codeigniter中的object(stdClass)检索结果

[英]how to retrieve result from object(stdClass) in codeigniter

I am trying to retrieve data from an sql query, in codeigniter. 我正在尝试从Codeigniter中的SQL查询检索数据。 but when I am trying to fetch query result ,I am getting only one field. 但是当我尝试获取查询结果时,我只得到一个字段。 In the query I have trying to get 2 fields. 在查询中,我尝试获取2个字段。 When I try to print the query result using var_dump the result I am getting is 当我尝试使用var_dump打印查询结果时,得到的结果是

object(stdClass)#33 (1) { ["product_id"]=> string(4) "1904" }

My Query is: 我的查询是:

$this->db->select("product_id","product_name")
            ->from('sale_items')
            ->where('sale_items.sale_id',4221);
            $q1 = $this->db->get();
            if ($q1->num_rows() > 0) {
            foreach (($q1->result()) as $row1) {
            $data1[] = $row1;
            }
            } else {
            $data1 = NULL;
            }
            echo "<br>";
            foreach($data1 as $prdtname)
            {   echo "<br>";
            echo var_dump($prdtname);
            echo "<br>";
            }

and the result is: 结果是:

object(stdClass)#32 (1) { ["product_id"]=> string(4) "1887" } 

object(stdClass)#33 (1) { ["product_id"]=> string(4) "1904" } 

As you see, I am not getting the second field that is product_name . 如您所见,我没有得到第二个字段product_name I do not know what is the problem with my query.Can anyone help me.. Thanks in advance. 我不知道我的查询有什么问题。任何人都可以帮助我..在此先感谢。

将您的选择语句更改为:

 $this->db->select("product_id ,product_name")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM