我使用PDO,我有一个需要添加LIKE语句的查询,但脚本失败,因为没有返回任何内容。一切都工作正常,直到我不得不将一个搜索项添加到查询字符串中,该搜索项将用于LIKE语句。
以下是代码:
<?php
include "connect.php";
$catId = 0;
$start = '';
$lastTS = '';
$searchItem = '';
if(isset($_REQUEST['Category'])){
$catId = $_REQUEST['Category'];
}
if(isset($_REQUEST['Start'])){
$start = $_REQUEST['Start'];
}
if(isset($_REQUEST['LastTS'])){
$lastTS = $_REQUEST['LastTS'];
}
if(isset($_REQUEST['SearchItem'])){
$searchItem = $_REQUEST['SearchItem'];
}
if($searchItem != ''){
$searchItem = '%' . $searchItem . '%';
}
if($start == 'start'){
if($catId > 0){
$q = $conn->prepare("SELECT t.*, u.U_FirstName, u.U_LastName, c.C_Name FROM T_Thread t INNER JOIN U_User u ON t.T_U_ID = u.U_ID INNER JOIN C_Category c ON t.T_C_ID = c.C_ID WHERE t.T_C_ID = :catId AND t.T_Name LIKE :searchItem ORDER BY t.T_ID DESC LIMIT 10");
$q->execute(array(':catId' => $catId, ':searchItem' => $searchItem));
}else{
$q = $conn->prepare("SELECT t.*, u.U_FirstName, u.U_LastName, c.C_Name FROM T_Thread t INNER JOIN U_User u ON t.T_U_ID = u.U_ID INNER JOIN C_Category c ON t.T_C_ID = c.C_ID WHERE t.T_Name LIKE :searchItem ORDER BY t.T_ID DESC LIMIT 10");
$q->execute(':searchItem' => $searchItem);
}
}else{
if($catId > 0){
$q = $conn->prepare("SELECT t.*, u.U_FirstName, u.U_LastName, c.C_Name FROM T_Thread t INNER JOIN U_User u ON t.T_U_ID = u.U_ID INNER JOIN C_Category c ON t.T_C_ID = c.C_ID WHERE t.T_C_ID = :catId AND t.T_Created > :lastTS AND t.T_Name LIKE :searchItem ORDER BY t.T_ID DESC");
$q->execute(array(':catId' => $catId, ':lastTS' => $lastTS, ':searchItem' => $searchItem));
}else{
$q = $conn->prepare("SELECT t.*, u.U_FirstName, u.U_LastName, c.C_Name FROM T_Thread t INNER JOIN U_User u ON t.T_U_ID = u.U_ID INNER JOIN C_Category c ON t.T_C_ID = c.C_ID WHERE t.T_Created > :lastTS AND t.T_Name LIKE :searchItem ORDER BY t.T_ID DESC");
$q->execute(array(':lastTS' => $lastTS, ':searchItem' => $searchItem));
}
}
$q->setFetchMode(PDO::FETCH_ASSOC);
while ($r = $q->fetch()) {
$threads[] = $r;
}
$arr = array('thread' => $threads);
echo json_encode($arr);
$conn = null;
?>
如果我使用这段代码,没有搜索项,它可以正常工作:
$q = $conn->prepare("SELECT t.*, u.U_FirstName, u.U_LastName, c.C_Name FROM T_Thread t INNER JOIN U_User u ON t.T_U_ID = u.U_ID INNER JOIN C_Category c ON t.T_C_ID = c.C_ID ORDER BY t.T_ID DESC LIMIT 10");
$q->execute();
但是当我在搜索项目中使用它时,不起作用:
$q = $conn->prepare("SELECT t.*, u.U_FirstName, u.U_LastName, c.C_Name FROM T_Thread t INNER JOIN U_User u ON t.T_U_ID = u.U_ID INNER JOIN C_Category c ON t.T_C_ID = c.C_ID WHERE t.T_Name LIKE :searchItem ORDER BY t.T_ID DESC LIMIT 10");
$q->execute(':searchItem' => $searchItem);
如果我对该值进行硬编码,一切正常:
$q = $conn->prepare("SELECT t.*, u.U_FirstName, u.U_LastName, c.C_Name FROM T_Thread t INNER JOIN U_User u ON t.T_U_ID = u.U_ID INNER JOIN C_Category c ON t.T_C_ID = c.C_ID WHERE t.T_Name LIKE '%uu%' ORDER BY t.T_ID DESC LIMIT 10");
$q->execute();
答案 0 :(得分:3)
您的语法在以下行中是错误的:
$q->execute(':searchItem' => $searchItem);
尝试:
$q->execute(array(':searchItem' => $searchItem));
答案 1 :(得分:0)
...
$q->execute(array(':searchItem' => '%'.$searchItem.'%'));