0
votes

I want to use replace function in mysql query using codeigniter but got stuck its not working

function getmakeid($make_name)
{

    $content_query = $this->db->query("select Make_ID from make where LOWER(REPLACE(Make_name, ' ', '')) = ".$make_name);
    $content_query = $this->db->get();
    if($content_query->num_rows() > 0)
    {
        return $content_query->result();
    }else{
        return false;
    }
}

returning an error

Error Number: 1054

Unknown column 'A3Cabriolet' in 'where clause'

select
  model_id
from model
where LOWER(REPLACE(model_name, ' ', '')) = A3Cabriolet

Filename: C:\wamp\www\system\database\DB_driver.php Line Number: 330"

3

3 Answers

2
votes

Try wrapping your query with commas like this

$query  =   "SELECT
              Make_ID
            FROM make
            WHERE LOWER(REPLACE(Make_name, ' ', '')) = "."'"$make_name."'";

$content_query = $this->db->query($query);  

Also when you have run the query once why do you need this

$content_query = $this->db->get();
0
votes

Syntax error: Your missing a "." before $make_name it should be

$query  =   "SELECT
              Make_ID
            FROM make
            WHERE LOWER(REPLACE(Make_name, ' ', '')) = '" . $make_name . "'";

$content_query = $this->db->query($query); 
0
votes

You can also use active record which imo is the best option.

$this->db->select('Make_ID')->
           from('make')->
           where("LOWER(REPLACE(Make_name,' ',''))",$make_name,TRUE);

$query = $this->db->get();

if($query->num_row() >= 1) {
    return $query->result();
}else {
    return TRUE;
}