【问题标题】:LIKE keyword with PDO execute query wont work带有 PDO 执行查询的 LIKE 关键字不起作用
【发布时间】:2015-05-25 17:30:29
【问题描述】:

它返回错误,我已经通过这个论坛上的其他几个答案尝试了一切,双引号,单引号......但仍然很难。有人请解释一下。

谢谢。

$query_string = $this->conn->prepare('select category_id, thread_id, thread_title, thread_body from thread where thread_title LIKE :thread_title OR thread_body LIKE :thread_body');
$query_string->execute(array(':thread_title'=>"%$this->search_this%", ':thread_body'=>"%$this->search_this%"));

【问题讨论】:

  • 将参数绑定为字符串
  • 那么->search_this 包含什么值?它是否适用于文字/字符串混搭查询?
  • @mario 真的没什么太有趣的。 $this->search_this = $_GET["search_query"];我向上帝发誓,我会在几小时前做同样的事情/查询完美地工作......
  • 可能是输入中的空格之类的。尝试使用静态字符串进行测试。如果没有可用的实际数据库,恐怕这是无法回答的;因为代码看起来很不错。
  • 令人尴尬的是,让我继续说这个。我已经从文件中删除了 db pass 和用户,所以我可以将它上传到 github ......并且忘记了重新向文件添加信息。 .“没有实际的数据库可用”给了我一个线索......谢谢@mario

标签: php sql database pdo prepared-statement


【解决方案1】:

类似 SQL 的查询就像

从线程中选择 category_id、thread_id、thread_title、thread_body WHERE thread_title LIKE '主题标题';

或者你可以这样写 从线程中选择 category_id、thread_id、thread_title、thread_body WHERE thread_title LIKE 'Thread Title%';

这意味着您的搜索字符串位于 ''(单引号)之间

我没有像你这样使用pdo sql代码,我是这样写的

$query_string = $this->conn->prepare("select category_id, thread_id, thread_title, thread_body from thread where thread_title LIKE '%".$this->search_this."%' OR thread_body LIKE '%".$ this->search_this."%'"); $query_string->execute();

【讨论】:

  • 这不是一个很好的答案或我在寻找什么先生。还是谢谢
【解决方案2】:

试试看

$query_string = $this->conn->prepare('select category_id, thread_id, thread_title, thread_body from thread where thread_title LIKE :thread_title OR thread_body LIKE :thread_body');
$query_string->execute(array(':thread_title'=>'%'.$this->search_this.'%', ':thread_body'=>'%'.$this->search_this.'%'));

如果有任何问题,请告诉我。

【讨论】:

  • 我在您的答案列表中没有看到任何类似的答案。试试看。
猜你喜欢
  • 2015-02-26
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多