Misti
Misti

Reputation: 71

Add array rows from database to one variable

I have a problem on CodeIgniter and MySQL logic maybe. I have two queries, I have executed them, each query has a different array variable and I want to put in one variable.

$query1 = $this->db->query("select a from base where id=1");
$query2 = $this->db->query("select b from base where id=2");
$this->data['result'] = $query1->result_array();
$this->data['result'] = $query2->result_array();

It's overwritten.

Upvotes: 0

Views: 80

Answers (3)

Extrakun
Extrakun

Reputation: 19305

You can try an array_merge too:

$final = array_merge($query1->result_array(),$query1->result_array());

This works only provided that result_array() returns an index-based array (that is, not an associative array)

Upvotes: 0

Suman Singh
Suman Singh

Reputation: 1377

I think you can use it like

$query = $this->db->query("select a from base where id IN(1,2)");

Than use $this->data['result'];

Upvotes: 0

NullPoiиteя
NullPoiиteя

Reputation: 57322

You are rewriting $this->data['result'] you need to add as sub array like

$this->data['result'][] = $query1->result_array();
$this->data['result'][] = $query2->result_array();

so now it will like

$this->data['result'][query1_result_array,query2_result_array]

Upvotes: 1

Related Questions