简体   繁体   中英

Search entire table for a keyword

I have a trivial question. Im using PHP+MySQL managing a huge DB

I want to search in a entire table a keyword I write in a input.

The problem is that the main table have +100 columns, so I had to write the php query manually

[...]
$sql="select * 
from db 
where ID LIKE '%".$q."%' or USER_ID LIKE '%".$q."%' or Phone_ID LIKE '%".$q."%' or
Fax_ID LIKE '%".$q."%' or email_ID LIKE '%".$q."%' or [...]

And this is a chaos when I modify a column, or add/remove...

Exist any other way to make this search? If not, I tought about create a separate PHP function, that obtains all column header names, and create an auto-fill function inside.

I tried to look for info with no success https://stackoverflow.com/search?q=search+entire+table

You could get the column info for the 'main table' using info from the information schema . Here are some methods for using MySQL. Here is how to do it using PHP.

Unfortunately there isnt any simple way to do this.

One option is to select all columns in table, fetch them as array and iterate over them and build your WHERE clause.

select column_name from information_schema.columns
where table_name = 'TableName'

This will make whole script slower, if you want to go this way i would recommend you to use some caching.

您可以在表上执行“ 显示列”,然后在“ Field上循环以获取表中的所有列名,至少这样可以避免手工编写的混乱。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM