简体   繁体   English

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

[英]error select in select query on codeigniter

I am new to codeigniter. 我是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; 我收到以下错误:您的SQL语法错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第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 -- 如果您使用的是框架,则必须遵循上述框架语法,答案是正确的,但如果使用的是核心概念,则以核心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();

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); 然后根据这将是非常长的代码,但是通过使用ci方法,我们可以在一行中完成它- $this->db->insert('table_name',$data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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