Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
223 views
Welcome To Ask or Share your Answers For Others

1 Answer

Per Amelia’s comment, here is the answer:

$sSearch = '%bier%' ;

and

value LIKE :search

did the trick.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...