为什么查询不起作用?
在模型查询中 -
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,我的第一个问题是:
如果不是,查询将不返回任何行。
%
通配符的使用是针对like
,而不是针对=
。例如,如果您的squery
单词为banāns
,则您的查询将仅匹配所有三列都完全为文字的行>%banāns%
值,不是其中包含banāns
的值。我想你想要:
对于你的后续问题:
如果是这种情况,您必须将查询更改为:
换句话说,移动 的附件参数规范中的
%
字符,而不是查询本身,从您的调试中,参数会自动用单引号括起来,因此,如果您想在这些参数内执行任何操作。引号,您必须在换行之前执行此操作,并且在这种情况下显然不需要双引号,因为正在进行换行
并使用
"%$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:
If not, the query will return no rows.
The use of the
%
wildcards is forlike
, not for=
. If yoursquery
word isbanāns
for example, your query will only match rows where all three columns are exactly the literal%banāns%
value, not a value containingbanāns
somewhere within it.I think you want:
For your subsequent problem:
If that's the case, you will have to change your query to something like:
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 :-)