pdo mysql like_PHP PDO准备的语句-MySQL LIKE查询
我正在嘗試通過php的PDO類(mysql驅(qū)動程序)進行搜索。我在MySQL客戶端上使用以下查詢(更改表名以保護無辜者):
SELECT? ? hs.hs_pk,
hs.hs_text,
hs.hs_did,
hd.hd_did,
hd.hd_text,
hv.hv_text,
hc.hc_text
FROM? ? ? hs
LEFT JOIN hd
ON? ? ? ?hs.hs_did = hd.hd_did
LEFT JOIN hd
ON? ? ? ?hd.hd_vid = hv.hv_id
LEFT JOIN hc
ON? ? ? ?hd.hd_pclass = hc.hc_id
WHERE? ? ?hs.hs_text LIKE "%searchTerm%"
LIMIT 25;
不管我使用什么搜索詞,它都像一個符咒。但是,當(dāng)我轉(zhuǎn)到php時,無法獲得返回任何內(nèi)容的信息。我嘗試了幾種看起來合乎邏輯的語法,但是沒有嘗試過。這是我現(xiàn)有的代碼:
$handle = fopen('/foo/bar/test.log', 'w+');
fwrite($handle, "doSearch, with search term: $searchTerm\n");
$sql =
'SELECT? ?hs.hs_pk,
hs.hs_text,
hs.hs_did,
hd.hd_did,
hd.hd_text,
hv.hv_text,
hc.hc_text
FROM? ? ? hs
LEFT JOIN hd
ON? ? ? ?hs.hs_did = hd.hd_did
LEFT JOIN hd
ON? ? ? ?hd.hd_vid = hv.hv_id
LEFT JOIN hc
ON? ? ? ?hd.hd_pclass = hc.hc_id
WHERE? ? ?hs.hs_text LIKE :searchTerm
LIMIT 25';
try {
$dbh = new PDO('mysql:host=localhost;dbname=awdb', "user", "password");
fwrite($handle, "connected to DB\n");
$prep = $dbh->prepare($sql);
$ret = $prep->execute(array(':searchTerm' => '"%'.$searchTerm.'%"'));
while ($row = $prep->fetch(PDO::FETCH_ASSOC)) {
$i++;
$result[$i]['subText'] = $row['hs_pk'];
$result[$i]['subText'] = $row['hs_text'];
$result[$i]['subDid'] = $row['hs_did'];
$result[$i]['devDid'] = $row['hd_did'];
$result[$i]['devText'] = $row['hd_text'];
$result[$i]['vendorText'] = $row['hv_text'];
$result[$i]['classText'] = $row['hc_text'];
}
$dbh = null;
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "
";
die();
}
我也嘗試了以下操作(SQL WHERE子句和prep-> execute行已全部更改):
WHERE hs.hs_text LIKE CONCAT(\'%\', ?, \'%\')
$ret = $prep->execute(array($searchTerm));
WHERE hs.hs_text LIKE "%:searchTerm%"
$ret = $prep->execute(array(':searchTerm' => $searchTerm));
WHERE hs.hs_text LIKE ":searchTerm"
$ret = $prep->execute(array(':searchTerm' => '%'.$searchTerm.'%'));
等等...
總結(jié)
以上是生活随笔為你收集整理的pdo mysql like_PHP PDO准备的语句-MySQL LIKE查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快速排序及查找第K个大的数。
- 下一篇: mysql 数据结构设计_MYSQL 设