dqyy38265 2015-05-20 23:16
浏览 72
已采纳

PDO BindParam没有使用Like语句

This is my code:

$sch = '%test%';

$select_search_query = "(
        SELECT 
            GROUP_CONCAT(name, ' ', surname) AS result
          FROM users
        WHERE 
        (
            email LIKE :sch OR 
            name LIKE :sch OR 
            surname LIKE :sch
        ) 
        AND 
            id_p = :ff_z
    )";

$prep_search_query = $connection->prepare($select_search_query);

$prep_search_query->bindParam(":sch", $sch);
$prep_search_query->bindParam(":ff_z", $id_p, PDO::PARAM_INT);

$prep_search_query->execute();

$array_search = $prep_search_query->fetchAll(PDO::FETCH_ASSOC);

print_r($array_search);

// The output is empty

After spending a pair of hours, searching for the possible mistakes, I finally decided to come out here, because I don't know why this is happening...

I'm programming a search page and it must works using PDO. The above code, doesn't works, however, the following one does:

//$sch = '%test%';

$select_search_query = "(
        SELECT 
            GROUP_CONCAT(name, ' ', surname) AS result
          FROM users
        WHERE 
        (
            email LIKE '%test%' OR 
            name LIKE '%test%' OR 
            surname LIKE '%test%'
        ) 
        AND 
            id_p = 1
    )";

$prep_search_query = $connection->prepare($select_search_query);

$prep_search_query->execute();

$array_search = $prep_search_query->fetchAll(PDO::FETCH_ASSOC);

print_r($array_search);

// The output is a very long array

It seems the problem is located in the LIKE, but I don't find the solution, I've tested many things and got no results. Thanks!

  • 写回答

1条回答 默认 最新

  • dsf323233323332 2015-05-20 23:19
    关注

    Unfortunately you can't reuse same placeholder when PDO bindParam like you did:

    email LIKE :sch OR 
    name LIKE :sch OR 
    surname LIKE :sch
    

    it should be something like:

    email LIKE :sch1 OR 
    name LIKE :sch2 OR 
    surname LIKE :sch3
    

    and then underneath:

    $prep_search_query->bindValue(":sch1", $sch); //or bindParam I guess will work as well
    $prep_search_query->bindValue(":sch2", $sch);
    $prep_search_query->bindValue(":sch3", $sch);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效