由于我刚开始使用 PDO 并且在偏离简单的 select from 查询时遇到问题,我想我最好在这里询问.
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.
代码:
$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 的原因.
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.
但是,该查询已经用 $sSearch
替换为一些搜索值进行了测试并且可以正常工作,但是现在我已经使用 PDO 执行相同的查询,并且得到一个空白结果.嗯,真的是一个空数组.
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?
根据 Amelia 的评论,答案如下:
Per Amelia’s comment, here is the answer:
$sSearch = '%bier%' ;
和
value LIKE :search
成功了.
这篇关于PDO 'LIKE' 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!