phpcodeigniterwhere-clausequery-builder

CodeIgniter: Unknown column xxx in ‘where clause’


I am working on a bug(?) for a few hours now, but couln’t fix it.

This is my code:

 $this->db->get_where('merken',array('m_merken' => $brand))->count_all_results();

$brand contains Acer in this preview.

 A Database Error Occurred

    Error Number: 1054

    Unknown column ‘Acer’ in ‘where clause’

    SELECT * FROM (`merken`) WHERE `m_name` = Acer 

I want to check if it already exists, but it won’t work very well.


Solution

  • Without quotes, your statement:

    SELECT * FROM (`merken`) WHERE `m_name` = Acer
    

    Acer refers to a column-name. If your intent is a string literal, put it in single-quotes, as in:

    SELECT * FROM (`merken`) WHERE `m_name` = 'Acer'
    

    Also, as a matter of good programming practice, avoid SELECT *, better to SELECT each column you want to return, even if the list is lengthy.

    -- EDIT --

    I suspect I'm missing the point... the SQL is generated. Two things to check:

    Is m_name correctly declared as a string/varchar/char field? Failing that, try literally setting the brand name to 'Acer', with the quote marks. I doubt this is a reasonable solution, though.