Search using REPLACE in a SELECT with PDO and .MDB ACCESS, with PHP

萝らか妹 提交于 2021-01-28 03:51:54

问题


I'm trying to write a mysql query that will match names from a table and the name in the database can contain dots or no dots. So, for example I would like my query string fast to match all of these: fast, f.ast, f.a.s.t etc.

I use PHP, with PDO connecting to a .MDB database.

I tried what I found here, with no success (I get error):

SELECT * FROM table WHERE replace(col_name, '.', '') LIKE "%fast%"

I think PDO for MDB databases is missing some functions :(
Any solution?


回答1:


Thanks to Doug, I solved with:

$variable = implode("[.]", str_split($variable)) . "[.]";

and:

SELECT * FROM table
WHERE
   col_name  LIKE "%" . $variable ."%";



回答2:


You cannot run the replace() function unless you are running the query through Access itself. You do however have a possible alternative, try the following:

SELECT * FROM table
WHERE
       col_name  LIKE "%fast%"
    OR col_name  LIKE "%f[.]a[.]s[.]t%";

The square brackets define an optional .

Or alternatively do it at PHP level with:

str_replace('.','',$var);


来源:https://stackoverflow.com/questions/45005856/search-using-replace-in-a-select-with-pdo-and-mdb-access-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!