PelnaE
PelnaE

Reputation: 69

Why query is not working?

In model query -

public function get_articles_from_query($squery){

    $query = DB::query(Database::SELECT, 'SELECT * FROM ieraksti WHERE virsraksts = "%:squery%" AND slug = "%:squery%" AND saturs = "%:squery%"')
            ->parameters(array(":squery" => $squery))->execute();
    return $query;
} 

Why this script is not working? I not have any errors, but blog articles not found.

Upvotes: 0

Views: 69

Answers (1)

paxdiablo
paxdiablo

Reputation: 881523

Well, my first question is:

Do you have any rows that have that word, and nothing else, surrounded by % characters, in all three of the the title, content and slug (a)? This seems unlikely.

If not, the query will return no rows.

The use of the % wildcards is for like, not for =. If your squery word is banāns for example, your query will only match rows where all three columns are exactly the literal %banāns% value, not a value containing banāns somewhere within it.

I think you want:

SELECT * FROM ieraksti
    WHERE virsraksts like "%:squery%"
      AND slug       like "%:squery%"
      AND saturs     like "%:squery%"

For your subsequent problem:

I understand the problem. I put in model var_dump($query); and it shows the query - SELECT * FROM ieraksti WHERE virsraksts like "%'ar'%" OR slug like "%'ar'%" OR saturs like "%'ar'%. The is not word 'ar', but ar. How to remove ' from query?

If that's the case, you will have to change your query to something like:

$query = DB::query(Database::SELECT,
    'SELECT * FROM ieraksti WHERE virsraksts like :squery AND slug ...')
        ->parameters(array(":squery" => "%$squery%"))->execute();

In other words, move the attachment of the % characters to the parameter specification, not the query itself. From your debug, the parameters are automatically having single quotes wrapped around them so, if you want to do anything inside those quotes, you have to do it before the wrapping.

And you obviously don't need the double quotes in this case because that wrapping is taking place.

And check that line with "%$squery%" in it, I'm not sure it's the right syntax. I'd say my PHP was a bit rusty but, to be honest, I've only ever used it for about seven minutes in my whole life :-)

What you're aiming for is a string consisting of %, the value of $squery and another %.


(a) Man tiešām patīk, kā Google Translate padara mani izskatās es zinu desmitiem dažādās valodās :-)

Upvotes: 1

Related Questions