Here's a PHP script that can be run from the command line to show a database's schema, look for matching fields in different tables, and identify matching field pairs that may be poorly normalized.
...
<?php
// command -d [database] -h [hostname] -u [user] -p [password] [fieldname] [ fieldname]
//
// If no field names are provided, displays list of all tables and fields
// If one field name, displays list of only tables that include at that field
// If two field names, displays list only of tables that include both field names
//
// https://bugs.nor1solutions.com/show_bug.cgi?id=2498
$dbname = 'dev';
$host = 'localhost';
$user = 'root';
$passwd = 'root';
for ($i = 1; $i < $_SERVER['argc']; $i++) {
if ($i === $db) {
$dbname = $_SERVER["argv"][$i];
continue;
}
if ($i === $hs) {
$host = $_SERVER["argv"][$i];
continue;
}
if ($i === $us) {
$user = $_SERVER["argv"][$i];
continue;
}
if ($i === $pw) {
$passwd = $_SERVER["argv"][$i];
continue;
}
if ($_SERVER["argv"][$i] == '-d') {
$db = $i + 1;
continue;
}
if ($_SERVER["argv"][$i] == '-h') {
$hs = $i + 1;
continue;
}
if ($_SERVER["argv"][$i] == '-u') {
$us = $i + 1;
continue;
}
if ($_SERVER["argv"][$i] == '-p') {
$pw = $i + 1;
continue;
}
if (!isset($fn))
$fn = $i;
if (isset($fn)) {
if ($i == $fn)
$m[0] = $_SERVER["argv"][$i];
if ($i == ($fn + 1 ))
$m[1] = $_SERVER["argv"][$i];
continue;
}
}
if (!mysql_connect($host, $user, $passwd)) {
echo 'Could not connect to mysql';
exit;
}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
mysql_select_db($dbname) or die("mysql_select() failed");
while ($row = mysql_fetch_row($result)) {
$query = "select * from ".$row[0]." limit 1";
if (!isset($m[0]))
echo "Table: {$row[0]}\n";
$res2 = mysql_query($query) or die(mysql_error());
$colqty = mysql_num_fields($res2);
$t = 0;
for ($i = 0; $i < $colqty; $i++) {
if (isset($_SERVER["argv"][2])) {
if (preg_match("/".$m[0]."/i", mysql_field_name($res2, $i))) {
$m1 = mysql_field_name($res2, $i); $t++;
}
}
if (isset($m[1])) {
if (preg_match("/".$m[1]."/i", mysql_field_name($res2, $i))) {
$m2 = mysql_field_name($res2, $i); $t++;
}
}
if (!isset($m[0]))
echo "\t".mysql_field_name($res2, $i)."\n";
}
if (isset($m[1])) {
if ($t == 2) {
echo "Table: {$row[0]}\n";
echo "\t".$m1."\n";
echo "\t".$m2."\n";
}
}
else if (isset($m[0])) {
if ($t == 1) {
echo "Table: {$row[0]}\n";
if (isset($m1)) echo "\t".$m1."\n";
if (isset($m2)) echo "\t".$m2."\n";
}
}
unset($m1);
unset($m2);
}
mysql_free_result($result);
?>