简体   繁体   English

PHP-使用LIKE语句的PDO

[英]PHP - PDO using the LIKE statement

Im using PDO, i have a query that needs to add the LIKE statement but the script fails as nothing is returned back. 我正在使用PDO,我有一个查询,需要添加LIKE语句,但是脚本失败,因为没有返回任何内容。 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. 一切工作正常,直到我不得不向查询字符串中添加一个搜索项,该搜索项将用于LIKE语句。

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;

    ?>

UPDATE 更新

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.'%'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM