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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备