PDO“喜欢”询问

发布于 2024-09-15 20:29:57 字数 1453 浏览 11 评论 0原文

由于我是使用 PDO 的新手,并且在偏离简单的查询选择时遇到问题,我想我最好在这里询问。

代码:

  $sDbase = str_replace('`', '', $modx->db->config['dbase']);
  $oPdo = new PDO("mysql:host=localhost;dbname=" . $sDbase . ";",   $modx->db->config['user'], $modx->db->config['pass']);
  $oPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  if(isset($_POST["search"]))
  {
    $sSearch = (!empty($_POST["search"])) ? mysql_real_escape_string($_POST["search"]) : "" ;
  }

  $sSearch = 'beer' ;

  $sQry = <<< QRY
    SELECT
      contentid AS standid
      ,value AS found
      ,pagetitle
      ,published
    FROM 
      modx_site_tmplvar_contentvalues
    RIGHT JOIN
      modx_site_content
      ON
      modx_site_content.id = modx_site_tmplvar_contentvalues.contentid
    WHERE
      ( tmplvarid = 41 OR tmplvarid = 40 )
    AND
      (value LIKE '%:search%'
      OR
      pagetitle LIKE '%:search%')
    AND
      published = '1'
    ORDER BY
      pagetitle
    ASC
QRY;

  $oRes = $oPdo->prepare( $sQry );
  $oRes -> bindParam( ":search", $sSearch );
  $oRes -> execute() ;

  $aRow = $oRes->fetchAll();
  $oRes -> closeCursor();


  var_dump($aRow);

变量 $sSearch 是我想要在查询中绑定到 :search 的变量。对于这个例子,我在其中设置了一个值,但显然我想用 POST 变量替换它,这就是我首先要使用 PDO 的原因。

但是,该查询已使用 $sSearch 进行了测试,并替换为一些搜索值并且有效,但现在我使用 PDO 执行相同的查询,但得到了空白结果。嗯,确实是一个空数组。

那么我在这里缺少什么?

Since I'm new to using PDO and running into a problem when deviating from a simple select from query, I figured I'd best ask around here.

The code:

  $sDbase = str_replace('`', '', $modx->db->config['dbase']);
  $oPdo = new PDO("mysql:host=localhost;dbname=" . $sDbase . ";",   $modx->db->config['user'], $modx->db->config['pass']);
  $oPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  if(isset($_POST["search"]))
  {
    $sSearch = (!empty($_POST["search"])) ? mysql_real_escape_string($_POST["search"]) : "" ;
  }

  $sSearch = 'beer' ;

  $sQry = <<< QRY
    SELECT
      contentid AS standid
      ,value AS found
      ,pagetitle
      ,published
    FROM 
      modx_site_tmplvar_contentvalues
    RIGHT JOIN
      modx_site_content
      ON
      modx_site_content.id = modx_site_tmplvar_contentvalues.contentid
    WHERE
      ( tmplvarid = 41 OR tmplvarid = 40 )
    AND
      (value LIKE '%:search%'
      OR
      pagetitle LIKE '%:search%')
    AND
      published = '1'
    ORDER BY
      pagetitle
    ASC
QRY;

  $oRes = $oPdo->prepare( $sQry );
  $oRes -> bindParam( ":search", $sSearch );
  $oRes -> execute() ;

  $aRow = $oRes->fetchAll();
  $oRes -> closeCursor();


  var_dump($aRow);

The variable $sSearch is what I want to bind to :search in the query. For this example, I've set a value in it, but obviously I want to replace that with a POST variable which is why I want to use PDO in the first place.

However, the query has been tested with $sSearch replaced by some searchvalue and works, but now I've used PDO to perform the same query, and I get a blank result. Well, an empty array really.

So what am I missing here?

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

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

发布评论

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

评论(1

后知后觉 2024-09-22 20:29:57

根据阿米莉亚的评论,答案是:

$sSearch = '%bier%' ;

并且

value LIKE :search

成功了。

Per Amelia’s comment, here is the answer:

$sSearch = '%bier%' ;

and

value LIKE :search

did the trick.

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