0

在我的控制器中,我使用以下代码将数据传递给模型:

$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);

在我的模型中,我想做的是使用 gid 值作为 SQL 语句的一部分,如下所示:

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

显然这不起作用,所以我只是想知道如何从 $data 获取 gid 并在我的 SQL 语句中使用它?

测试使用

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

但是输出是:

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 [行数] => 0 [行数据] => )

4

4 回答 4

0

你可以试试:

$get_gid = $this->db->query('SELECT * FROM users WHERE gid = '.$data['gid'].');
于 2013-07-11T06:10:15.917 回答
0

你试过 gid = $data['gid']

我假设您的模型方法如下所示:

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

如果是,请尝试显示查询以查看 $data['gid'] 是否在那里可见

你可以试试

$this->db->get_compiled_select();

或查询运行后

$this->db->last_query();
于 2013-07-10T23:39:00.593 回答
0

试试这个方法:

$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;
}
于 2013-07-11T03:44:22.020 回答
0

您只是在查询后忘记了$query->result()

于 2014-05-27T11:11:25.720 回答