php - add two different query in codeigniter -
i want add 2 query not working. here's my
model page:
public function updatetable(){ $first= $this->db->query("select column1 table table_id='data1' , field_id=6"); // data type numeric. when query this, result 0.0 $second= $this->db->query("select column1 table table_id='data2' , field_id=6"); // data type numeric also. when query this, result 0.0 $total=$first+$second; }
when try run got error message
message: object of class ci_db_postgre_result not converted int
how can make happen or possible?
edit: new info
i want use $total if statement .
if ($total==0){ //code here }
what should do?
let's see first example in ci userguide: http://ellislab.com/codeigniter/user-guide/database/results.html
$query = $this->db->query("your query"); foreach ($query->result() $row) { echo $row->title; echo $row->name; echo $row->body; }
we learn 2 things:
$this->db->query("your query")
not return result,$query->result()
does.- the result array of object. object names column name.
if you're sure every query return 1 result. can change code this:
$first= $this->db->query("select column1 table table_id='data1' , field_id=6")->result()[0]->column1; $second= $this->db->query("select column1 table table_id='data2' , field_id=6")->result()[0]->column1;
===== answer been revised below =====
the analysis correct code above throws error, because php regard result()[0] whole.
you can either separate them
$row = $this->db->query("select column1 table table_id='data1' , field_id=6")->result(); $first = $row[0]->column1;
or use current function
$first= current($this->db->query("select column1 table table_id='data1' , field_id=6")->result())->column1;
Comments
Post a Comment