为什么查询不起作用?

发布于 2024-12-11 15:20:44 字数 373 浏览 0 评论 0原文

在模型查询中 -

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;
} 

为什么这个脚本不起作用?我没有任何错误,但找不到博客文章。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗忘曾经 2024-12-18 15:20:44

嗯,我的第一个问题是:

标题的所有三个中是否有任何行包含该单词,没有其他内容,且由 % 字符包围、内容和段(a)?这似乎不太可能。

如果不是,查询将不返回任何行。

% 通配符的使用是针对like,而不是针对=。例如,如果您的 squery 单词为 banāns,则您的查询将仅匹配所有三列都完全文字的行> %banāns% 值,不是其中包含 banāns 的值。

我想你想要:

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

对于你的后续问题:

我明白这个问题了。我放入模型 var_dump($query);它显示查询 - SELECT * FROM ieraksti WHERE virsraksts like "%'ar'%" OR slug like "%'ar'%" OR saturs like "%'ar'%。单词 'ar',但是 ar. 如何从查询中删除 '?

如果是这种情况,您必须将查询更改为:

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

换句话说,移动 的附件参数规范中的 % 字符,而不是查询本身,从您的调试中,参数会自动用单引号括起来,因此,如果您想在这些参数内执行任何操作。引号,您必须在换行之前执行此操作,

并且在这种情况下显然不需要双引号,因为正在进行换行

并使用 "%$squery%" 检查该行 。它,我不确定这是正确的语法。 PHP 有点生疏,但说实话,我一生中只使用过它大约七分钟:-)

你的目标是一个由 % 组成的字符串, $squery 和另一个 % 的值。


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

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 :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文