简体   繁体   中英

how to retrieve result from object(stdClass) in codeigniter

I am trying to retrieve data from an sql query, in codeigniter. 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. When I try to print the query result using var_dump the result I am getting is

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 . 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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