0

Here is my selection code from db:

$q = $this->db->like('Autor1' or 'Autor2' or 'Autor3' or 'Autor4', $vyraz)
    ->where('stav', 1)
    ->order_by('id', 'desc')
    ->limit($limit)
    ->offset($offset)
    ->get('knihy');
                    
return $q->result();

Where $vyraz = "Zuzana Šidlíková";

And the error is:

Nastala chyba databázy

Error Number: 1054

Unknown column '1' in 'where clause'

SELECT * FROM (\knihy`) WHERE `stav` = 1 AND `1` LIKE '%Zuzana Šidlíková%' ORDER BY `id` desc LIMIT 9

Filename: C:\wamp\www\artbooks\system\database\DB_driver.php

Line Number: 330

Can you help me solve this problem?

4

1 Answer 1

1

Your syntax is wrong for what you're trying to do, but still technically valid, because this:

'Autor1' or 'Autor2' or 'Autor3' or 'Autor4'

...is actually a valid PHP expression which evaluates to TRUE (because all non-empty strings are "truthy"), which when cast to a string or echoed comes out as 1, so the DB class is looking to match on a column called "1".

Example:

function like($arg1, $arg2)
{
    return "WHERE $arg1 LIKE '%$arg2%'";
}

$vyraz = 'Zuzana Šidlíková';
echo like('Autor1' or 'Autor2' or 'Autor3' or 'Autor4', $vyraz);

// Output: WHERE 1 LIKE '%Zuzana Šidlíková%'

Anyways, here's what you need:

$q = $this->db
    ->like('Autor1', $vyraz)
    ->or_like('Autor2', $vyraz)
    ->or_like('Autor3', $vyraz)
    ->or_like('Autor4', $vyraz)
    ->where('stav', 1)
    ->order_by('id', 'desc')
    ->limit($limit)
    ->offset($offset)
    ->get('knihy');
Sign up to request clarification or add additional context in comments.

5 Comments

What do you mean "not working"? More information - as much as possible. Same error message? Not producing the query you want? Not returning results you expect? Exactly what do you mean?
I see no reason why that would be true, are you 100% sure you aren't just running your original code again? Try commenting out parts of it to see where the problem comes from. Do you understand the error I pointed out?
I am absolutely sure that I run your new code. I tried it with die(); adding die(); on different position unlit error disappeared and problem is in querry
I can't understand why this code would produce the same exact query as your original broken/invalid code. What specifically was the part you commented out that got the error to disappear?
You was right I have problem in different part of code :) THANKS A LOT! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.