Created
March 28, 2017 06:25
-
-
Save joeke/10a43372beb3b1983b3b2945b502be2c to your computer and use it in GitHub Desktop.
PHP search all mysql tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function findAll($search_str, $database, $types = ['tinytext','blob','varchar','text','longblob']){ | |
global $pdo; | |
foreach($types as &$t){ | |
$t = "'" . $t . "'"; | |
} | |
$types = implode(',',$types); | |
$s = $pdo->prepare(" | |
SELECT TABLE_NAME, COLUMN_NAME | |
FROM INFORMATION_SCHEMA.columns | |
WHERE | |
TABLE_SCHEMA = :database | |
AND DATA_TYPE IN ($types) | |
"); | |
$s->bindValue(':database', $database); | |
$s->execute(); | |
$results = []; | |
foreach($s->fetchAll(\PDO::FETCH_ASSOC) as $column){ | |
$search = $pdo->prepare("SELECT * FROM $database.${column['TABLE_NAME']} WHERE ${column['COLUMN_NAME']} LIKE :search_str"); | |
$search->bindValue(':search_str', $search_str); | |
$search->execute(); | |
$result = $search->fetchAll(\PDO::FETCH_ASSOC); | |
if(count($result)) | |
$results[$column['TABLE_NAME']] = isset($results[$column['TABLE_NAME']]) | |
? array_merge( | |
$results[$column['TABLE_NAME']], | |
$result | |
) | |
: $result; | |
} | |
return $results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment