PDO“喜欢”询问
由于我是使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据阿米莉亚的评论,答案是:
并且
成功了。
Per Amelia’s comment, here is the answer:
and
did the trick.