<?php

/**
 * Database Schema Search Utility
 * 
 * Searches on column name for all tables in all databases on multiple MySQL 
 * servers. To use simply configure the $host array with connection information 
 * on all servers you wish to search and then upload file to a php enabled web 
 * server. You can then access the script via a web browser and do searching.
 * 
 * WARNING: It is not wise to leave this script accessible by the public as it 
 * provides window for a malicious user to gather information about your 
 * database structure. You should protect in some way that it is only 
 * accessible by authorized users.
 * 
 * @author Jordon Mears <jordoncm at gmail dot com>
 * @copyright Copyright (c) 2005, Jordon Mears
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
 * @link http://www.finefrog.com/projects#utilities
 * @version 2.1
 */

if(!isset($_REQUEST['idd'])) {
    
$_REQUEST['idd'] = 1;
}

$default_databases = array('mysql''information_schema');

$host   = array();
$host[] = array(
    
'server' => 'localhost',
    
'user' => 'user',
    
'password' => 'password'
);

$output   = array();
$output[] = '<html>';
$output[] = '<head>';
$output[] = '<title>database schema search</title>';
$output[] = '</head>';
$output[] = '<body>';
// $output[] = '<pre>' . print_r($host, true) . '</pre>';

for($i 0$i count($host); $i++) {
    
$conn mysql_connect($host[$i]['server'], $host[$i]['user'], $host[$i]['password']);
    
$dbsql 'SHOW DATABASES';
    
$dbresult mysql_query($dbsql$conn);
    
$dbnum_rows mysql_num_rows($dbresult);
    
$db = array();
    for(
$j 0$j $dbnum_rows$j++) {
        
$dbrow mysql_fetch_array($dbresultMYSQL_NUM);
        
$db[$j]['db_name'] = $dbrow[0];
    }
    
$host[$i]['databases'] = $db;
    
// $output[] = '<pre>' . print_r($db, true) . '</pre>';
    
for($j 0$j count($host[$i]['databases']); $j++) {
        if(!
$_REQUEST['idd'] || !in_array(strtolower($host[$i]['databases'][$j]['db_name']), $default_databases)) {
            
mysql_select_db($host[$i]['databases'][$j]['db_name'], $conn);
            
$tablesql 'SHOW TABLES';
            
$tableresult mysql_query($tablesql$conn);
            
$tablenum_rows mysql_num_rows($tableresult);
            
$table = array();
            for(
$k 0$k $tablenum_rows$k++) {
                
$tablerow mysql_fetch_array($tableresultMYSQL_NUM);
                
$table[$k]['table_name'] = $tablerow[0];
            }
            
$host[$i]['databases'][$j]['tables'] = $table;
            
// $output[] = '<pre>' . print_r($table, true) . '</pre>';
            
for($k 0$k count($host[$i]['databases'][$j]['tables']); $k++) {
                
$fieldsql 'SHOW COLUMNS FROM `'
                          
$host[$i]['databases'][$j]['tables'][$k]['table_name']
                          . 
'` LIKE "%' mysql_escape_string($_REQUEST['find']) . '%"';
                
$fieldresult mysql_query($fieldsql$conn);
                
                
$fieldnum_rows mysql_num_rows($fieldresult);
                
$field = array();
                for(
$l 0$l $fieldnum_rows$l++) {
                    
$fieldrow mysql_fetch_array($fieldresultMYSQL_ASSOC);
                    
$field[$l] = $fieldrow;
                }
                
$host[$i]['databases'][$j]['tables'][$k]['fields'] = $field;
                
// $output[] = '<pre>' . print_r($field, true) . '</pre>';
            
}
        }
    }
    
// $output[] = '<pre>' . print_r($host, true) . '</pre>';
}

$output[] = '<form action="" method="get">';
$output[] = 'search columns: ';
$output[] = '<input name="find" type="text" value="' $_REQUEST['find'] . '" />';
$output[] = '<input type="submit" value="search" />';
$output[] = '<input type="button" value="clear" onclick="window.location = window.location.pathname;" />';
$output[] = '<br />';
if(!
$_REQUEST['idd']) {
    
$output[] = '<span style="font-size: 80%;">';
    
$output[] = '<input checked="checked" name="idd" type="checkbox" value="0" /> search default tables (mysql, information_schema, ...)';
    
$output[] = '</span>';
} else {
    
$output[] = '<span style="font-size: 80%;">';
    
$output[] = '<input name="idd" type="checkbox" value="0" /> search default tables (mysql, information_schema, ...)';
    
$output[] = '</span>';
}
$output[] = '</form>';
$output[] = '<br />';

$output[] = '<table border="1" cellpadding="0" cellspacing="0">';
$output[] = '<thead>';
$output[] = '<tr>';
for(
$w 0$w count($host); $w++) {
    
$output[] = '<th>' $host[$w]['server'] .'</th>';
}
$output[] = '</tr>';
$output[] = '</thead>';
$output[] = '<tbody>';
$output[] = '<tr>';
for(
$w 0$w count($host); $w++) {
    
$bg 0;
    
$output[] = '<td valign="top">';
    
$output[] = '<table border="0" cellpadding="4" cellspacing="0" style="font-size: 85%;">';
    for(
$x 0$x count($host[$w]['databases']); $x++) {
        for(
$y 0$y count($host[$w]['databases'][$x]['tables']); $y++) {
            for(
$z 0$z count($host[$w]['databases'][$x]['tables'][$y]['fields']); $z++) {
                if(
$bg == 1) {
                    
$bgcolor '#CCCCCC';
                    
$bg++;
                } else {
                    
$bgcolor '#EEEEEE';
                    
$bg++;
                }
                
$output[] = '<tr>';
                
$output[] = '<td bgcolor="' $bgcolor '">';
                
$output[] = '<span style="color: red;">' $host[$w]['databases'][$x]['db_name'] . '</span>.';
                
$output[] = '<span style="color: green;">' $host[$w]['databases'][$x]['tables'][$y]['table_name'] . '</span>.';
                
$output[] = '<span style="color: blue;">' $host[$w]['databases'][$x]['tables'][$y]['fields'][$z]['Field'] . '</span>';
                
$output[] = '</td>';
                
$output[] = '<td bgcolor="' $bgcolor '">';
                
$output[] = $host[$w]['databases'][$x]['tables'][$y]['fields'][$z]['Type'];
                
$output[] = '</td>';
                
$output[] = '</tr>';
            }
        }
    }
    
$output[] = '</table>';
    
$output[] = '</td>';
}
$output[] = '</tr>';
$output[] = '<tbody>';
$output[] = '</table>';

$output[] = '</body>';
$output[] = '</html>';

echo 
join(''$output);

?>