简体   繁体   中英

error select in select query on codeigniter

I am new to codeigniter. When i use this query

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

I get the following error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

Try this

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

if you are using a framework then you have to follow framework syntax above answered is correct but it`s answered in core php format not in framework format if you will use core concept then why you are using framework leave it and use this query for select data --

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

Please Try This.

if i want to insert data like that --

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

then according this it will be very long code but by usng ci methods we can do it in a single line -- $this->db->insert('table_name',$data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM