简体   繁体   中英

SQL syntax checkbox result

I have a problem with a SQL query. I'm getting the result of 2 checkboxes selection. This give me one array. I need to query my DB in order to see if there is a statement corresponding to one of the attribute in the array. But I must search in two columns.

Here is my code :

    $ids = join(',',$param);  
    $this->db->select('*');
    $this->db->from('default_profiles');
    $test = " 'motivation' IN (".$ids.") OR 'envies' IN (".$ids.")";
    $this->db->where($test);

Here is my error : You have an error in your SQL syntax; Unknown column ' 'motivation' IN (option3,option3) OR 'envies' IN (option3,option3)' in 'where clause'

SELECT * FROM ( default_profiles ) WHERE ` 'motivation' IN (option3,option3) OR 'envies' IN (option3,option3)

If i remove the or statement it works but it only search in one column.

If someone can help me it would be great !

Update with single quote EDIT : I think the problem comes from the quote juste after the WHERE but I don't know how to delete it

MySQL does not accept single quotes for quoting table or column names, but expects backticks. You should check, if you have got any ids, otherwise you may run into trouble.

PHP 5.3:

if (!empty($param)) {
    $db = $this->db;
    array_walk($param, function(&$value) use ($db) {
        $value = "'" . $db->escape($value) . "'";
    });
    $ids = join(',', $param);
    $this->db->select('*');
    $this->db->from('default_profiles');
    $test = " `motivation` IN ($ids) OR `envies` IN ($ids)";
    $this->db->where($test);
    // ...
}

PHP 5.4:

if (!empty($param)) {
    array_walk($param, function(&$value) {
        $value = "'" . $this->db->escape($value) . "'";
    });
    $ids = join(',', $param);
    $this->db->select('*');
    $this->db->from('default_profiles');
    $test = " `motivation` IN ($ids) OR `envies` IN ($ids)";
    $this->db->where($test);
    // ...
}

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