FindTableNamesByFieldName
ElegantCoder::wiki()
개요
- 필드가 들어있는 테이블을 정규식으로 찾아줌
코드
<?php
$dbhost = 'localhost';
$dbid = '';
$dbpassword = '';
$dbname = '';
$ret = array();
mysql_connect($dbhost, $dbid, $dbpassword);
$toFind = $argv[1]? $argv[1]: $_GET['field'];
mysql_select_db($dbname);
$showTables_res = mysql_query("show tables");
while($tables = mysql_fetch_array($showTables_res)) {
list(,$tableName) = each($tables);
$showColumnsResult = mysql_query("SHOW COLUMNS FROM ".$tableName);
if (mysql_num_rows($showColumnsResult) > 0) {
while ($row = mysql_fetch_assoc($showColumnsResult)) {
if(preg_match('/'.$toFind.'/', $row['Field'])) {
$ret[$tableName] = true;
}
}
}
}
foreach($ret as $tableName => $val) {
echo $key."\n";
}
사용법
커멘드라인에서
$php findTableNamesByFieldName.php 찾을필드이름
또는 웹브라우저에서
findTableNameByFieldName.php?field=찾을필드이름