在codeigniter上的select查询中选择错误

时间:2015-11-10 05:36:44

标签: php mysql codeigniter

我是codeigniter的新手。当我使用此查询时

$last_insert_id =$this->db->insert_id();
    $name = 'Auto loan';
    $q = $this->db->select('id')->where('name_of_loan',$name)->limit(1)->get('loan_type');

$data_batch = array(
'borrower_id' => $last_insert_id,
'loan_type_id'=> $q,

);
//$this->db->set('loan_type_id', $query);
$this->db->set('created_on', 'NOW()', FALSE);
$this->db->set('updated_on', 'NOW()', FALSE);
$this->db->insert('loan_application',$data_batch);

我收到以下错误:  您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行的')'附近使用正确的语法

3 个答案:

答案 0 :(得分:0)

试试这个

$last_insert_id =$this->db->insert_id();
$name = 'Auto loan';
$query = $this->db->query("SELECT * FROM loan_type WHERE name_of_loan = '$name' ");
$result = $query->result_array();
$q = $result[0]['id'];

$data_batch = array(
    'borrower_id' => $last_insert_id,
    'loan_type_id'=> $q,        
);

答案 1 :(得分:0)

如果你正在使用一个框架,那么你必须遵循上面的框架语法回答是正确的但它以核心php格式回答而不是框架格式如果你将使用核心概念然后为什么你使用框架离开它并使用这个查询选择数据 -

     $name = 'Auto loan';
     $this->db->select('id');
     $this->db->from('loan_type');
     $this->db->where('name_of_loan',$name);
     $this->db->limit(1);
     $query = $this->db->get();
     return $query->result_array();

请试一试。

答案 2 :(得分:0)

如果我想插入这样的数据 -

$sql = "INSERT INTO create_shop (shop_name,shop_category,address,location,city,state,postal_code,phone_no,email,min_ammount,description,user_id,image1,image2,image3,image4,date_time)
                VALUES(" . $this->db->escape($this->input->post('shop_name')) . "
                        ," . $this->db->escape($this->input->post('shop_category')) . "
                        ," . $this->db->escape($this->input->post('address')) . "
                        ," . $this->db->escape($this->input->post('location')) . "
                        ," . $this->db->escape($this->input->post('city')) . "
                        ," . $this->db->escape($this->input->post('state')) . "
                        ," . $this->db->escape($this->input->post('postal_code')) . "
                        ," . $this->db->escape($this->input->post('phone_no')) . "
                        ," . $this->db->escape($this->input->post('email')) . "
                        ," . $this->db->escape($this->input->post('min_ammount')) . "
                        ," . $this->db->escape($this->input->post('description')) . "
                        ,".$this->db->escape($this->session->userdata('username'))."
                        ,".$this->db->escape($f_newfile)."
                        ,".$this->db->escape($f_newfile1)."
                        ,".$this->db->escape($f_newfile2)."
                        ,".$this->db->escape($f_newfile3)."
                        ," . $this->db->escape($now) . ")";

然后据此,它将是非常长的代码,但通过usc ci方法,我们可以做 它在一行中 - $this->db->insert('table_name',$data);