0
votes

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] => )

4

4 Answers

0
votes

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();
0
votes

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;
}
0
votes

You can try:

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

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