dongyan1936 2015-06-15 16:51
浏览 114
已采纳

使用mysql,php和pdo选择不区分大小写

I'm trying to select some data from a mysql table but I cannot get the Where comparison to be case insensitive, I tried using LOWER:

$wildcard = $_GET['q'];
        $query = "SELECT id, name, departamento FROM gestionDoc_cargos WHERE (LOWER(name) LIKE '%' LOWER(:wildcard) '%' OR LOWER(departamento) LIKE '%' LOWER(:wildcard) '%')";

        try{
            $result = DB::getInstance()->prepare($query);
            $result->bindParam(':wildcard', $wildcard, PDO::PARAM_STR);
            $result->execute();
            $result = $result->fetchAll(PDO::FETCH_ASSOC);
        }catch(PDOException $e){
            die($e->getMessage());
        }
        print_r($result);
        echo json_encode(array_values($result));

but I get the following error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LOWER('C') '%' OR LOWER(departamento) LIKE '%' LOWER('C') '%')' at line 1

and if I remove the LOWER from the query I get a case sensitive select.

  • 写回答

1条回答 默认 最新

  • doumanju2533 2015-06-15 16:55
    关注

    This

    ...snip... ) LIKE '%' LOWER(:wildcard) '%' OR ...snip
    

    is incorrect. You've got a string ('%') followed by a function call (LOWER()) followed by another string, and they're just sitting there - no connecting logic, no concatenation, blah blah blah .

    It should be

    ... LIKE CONCAT('%', LOWER(:wildcard), '%') OR ...
    

    And by default, mysql comparisons ARE case insensitive, unless you force a binary comparison, or you're using a case sensitive collation on your db/table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献