Link to home
Start Free TrialLog in
Avatar of n00b0101
n00b0101

asked on

Mysql - Search for string in entire database

Is there a way to find a string through the whole database?
Avatar of Nem Schlecht
Nem Schlecht
Flag of United States of America image

Do you need it as part of an actual search or are you just looking for it this one time?

If the latter, you can just do a mysqldump on your entire database and look through the results.
mysqldump --extended-insert=0 my_database | less

Open in new window

mysql_list_tables($dbname) will give you the names of every table in the database to which you are connected.  

Do a while loop on that, and within the loop, for each table, mysql_list_fields($dbname, $tablename) will give you every fieldname in that table.  

Do another while loop on that, and within this loop, do a SELECT $fieldname FROM $tablename WHERE $fieldname LIKE '%$string%'

I should say, if the former, then not really, no.

You could maybe do a SELECT .. UNION on *all* of your tables and then specify a WHERE clause with something like CONCAT(table1.col1, table1.col2, table2.col1, table2.col2, ...) LIKE "%string%"

Not fun to write that query and it will be horribly slow (well, depending on how many tables/columns you have).
ASKER CERTIFIED SOLUTION
Avatar of theGhost_k8
theGhost_k8
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial