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

报告相同问题?

悬赏问题

  • ¥15 qt+ffmpeg报错non-existing PPS 0 referenced
  • ¥15 FOC simulink
  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?