Using mysql_grep.php to grep tables

I’ve created a new piece of code that allows people to grep through their database tables and fields. I regularily work on databases with more than 50 tables in the database. Each table has anywhere from two to twenty fields in them. To make things even more of a pain in the ass sometimes the fields are mispelled or do not match up.

As a result I created mysql_grep.php. You can download it from my newly created source directory. Check back often for more code/utilities created by me. Below is an example use of mysql_grep.php

jstump@zebulon:~$ php -q bin/mysql_grep.php -d joestump_net -e photo

The following fields match "photo"
  +-> photos_favorites
      +-> photoID
  +-> photos_images
      +-> photoID

The following tables match "photo"
  +-> photos_albums
  +-> photos_favorites
  +-> photos_images

The code does not support connecting to hosts other than localhost nor does it support changing sockets/ports. Furthermore, the code requires PEAR’s DB. And, finally, the expressions are passed directly to PHP’s ereg(). This means you should be able to pass "^[A-Z]" as an expression (remember to quote your complex expressions).

2 thoughts on “Using mysql_grep.php to grep tables

  1. You can run PHP scripts just like perl or shellscripts. Put
    #! /usr/bin/php -q
    on the first line, and give the script execute permission (chmod +x mysql_grep.php on most GNU systems). Much easier.

  2. Ian, you’re absolutely correct. One thing to note, though, is that the path to your CLI version of php might be different. Type “which php” to find the appropriate path first (most likely /usr/bin/php or /usr/local/bin/php).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.