K20GH
K20GH

Reputation: 6281

Use Array Data in CodeIgniter Model as SQL?

In my controller, I am passing data to the model using the following code:

$data = array(
    'gid'   =>  $this->input->post('gid'),
    'name'  =>  $this->input->post('name'),
    'pic'   =>  $this->input->post('pic'),
    'link'  =>  $this->input->post('link')
);  
var_dump($data);
$this->Login_model->insert_entry($data);

In my model, what I want to do is use the gid value as part of an SQL statement, like so:

$get_gid = $this->db->query('SELECT * FROM users WHERE gid = $gid');

Obviously this doesn't work, so I'm just wondering how I get the gid from $data and use it in my SQL statement?

Tested using

$get_gid = $this->db->where('gid', $data['gid'])->get('users');
print_r($get_gid);

However output is:

CI_DB_mysql_result Object ( [conn_id] => Resource id #30 [result_id] => Resource id #33 [result_array] => Array ( ) [result_object] => Array ( ) [custom_result_object] => Array ( ) [current_row] => 0 [num_rows] => 0 [row_data] => )

Upvotes: 0

Views: 145

Answers (4)

Ravi
Ravi

Reputation: 717

You just Forget after query $query->result().

Upvotes: 0

rfpdl
rfpdl

Reputation: 964

You can try:

$get_gid = $this->db->query('SELECT * FROM users WHERE gid = '.$data['gid'].');

Upvotes: 0

MJ X
MJ X

Reputation: 9054

Try this way:

$data = array(
 'gid'   =>  $this->input->post('gid'),
 'name'  =>  $this->input->post('name'),
 'pic'   =>  $this->input->post('pic'),
 'link'  =>  $this->input->post('link')

);

$gid = $data['gid'];

$this->db->where('gid',$gid);
$this->db->from('users');
$query = $this->db->get(); 
if($query)
{
    //you can return query or you can do other operations here like insert the array data
    //$this->db->insert('yourtable',$data);
    return $query;
}
else
{
    return FALSE;
}

Upvotes: 0

bidon
bidon

Reputation: 3

Did you try gid = $data['gid']

I assume that yours model method looks like this:

insert_entry($data)
{
     here active record or query...
 }

If yes try to display query to see if $data['gid'] is visible there

You can try it by

$this->db->get_compiled_select();

Or after query runs

$this->db->last_query();

Upvotes: 0

Related Questions