Im using PDO, i have a query that needs to add the LIKE statement but the script fails as nothing is returned back. Everything was working fine until i had to add a search item to the query string which this search item would be used for the LIKE statement.
Here is the code:
<?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;
?>
If i use this bit of code, without the search item it works fine:
$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();
But when i use this with search item, does not work:
$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);
And if i hard code the value, everything works:
$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();
Your syntax is wrong in the following line:
$q->execute(':searchItem' => $searchItem);
Try:
$q->execute(array(':searchItem' => $searchItem));
you should be doing:
...
$q->execute(array(':searchItem' => '%'.$searchItem.'%'));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.