#! /usr/bin/php
<?php
error_reporting(63);
/***************************************
** Title........: Search and replace utility in all MySQL db
** Filename.....: search.and.replace.php
** Author.......: Fred Radeff
** Version......: 0.0
** Notes........:
** Last changed.: 09/24/2008
** Last change..:
** Usage: mysqlcherchepartout.php [enter] [yourString] [enter]
***************************
echo "Entrez la chaine a chercher: ";
$chercher = trim(fgets(STDIN)); // lit une seule ligne depuis STDIN************/
if(strlen($chercher)<1) {
echo "Usage: mysqlcherchepartout.php [enter] [yourString] [enter]\n\n";
exit;
}
 
 
############ 
# mysql connection
include("connect.inc.php");
$db=connect_db();
 
############ 
# list all db
$listedb="show databases";
$result=@mysql_query($listedb);
#$result=mysql_list_dbs();
if (!$result) {
exit('<p>Sql error: ' . mysql_error() .
'</p>');
} 
#loop on db names
while ($row = mysql_fetch_array($result)) {
#echo '<p>' . $row[0] . '</p>';
$db=$row[0];
echo "-----------------------------------------";
 echo "\n\r";
 #echo "DB: "; //tests
 #echo $db; //tests
$utiliseDB=mysql_query("USE $db");
if(!$utiliseDB) {
echo "Erreur d'utilisation de la base $db: " .mysql_error();
}
 
	#search tablenames
	$tables = "show tables from $db";
	$result_tables = mysql_query($tables);
 
	if (!$result_tables) {
	echo "db error, could not list tables\n";
	echo 'mysql error: ' . mysql_error();
	exit;
	}
	#loop on tablenames
	while ($rowtables = mysql_fetch_row($result_tables)) {
	#echo " > Table: ";//tests
	$latable=$rowtables[0];
	$larequete="SELECT * FROM $db.$latable WHERE";
 
	#echo "{$rowtables[0]}\n";//tests

		#search fieldnames (cols)

		$champs = mysql_query("SHOW COLUMNS FROM $latable");
		if (!$champs) {
		echo 'Could not run query: ' . mysql_error();
		exit;
		}
		if (mysql_num_rows($champs) > 0) {
		while ($row_champs = mysql_fetch_row($champs)) {
			#echo " > > Champ: "; //tests
			#echo "{$row_champs[0]}\n"; //tests
			$lechamp=$row_champs[0];
			#add the field to the sql query
			$larequete.=" OR `$lechamp` LIKE '%$chercher%' \n";
		}
		}
			#now construct the sql query
			$larequeteSql=str_replace("WHERE OR", "WHERE", $larequete);
				#exec the sql query
				$larequete=mysql_query($larequeteSql);
				if (!$larequete) {
				echo "sql query error\n";
				echo 'mysql error: ' . mysql_error();
				}
				if(mysql_num_rows($larequete)>0){
				#echo "SQL: \n" .$larequeteSql ."\n\n"; //tests			
				#echo "Hits: " .mysql_num_rows($larequete) ."\n"; //tests
				echo "--------------\nOccurrences dans base / table: " .$db ." / " .$latable ."\n\n";
				$i=0;
				while ($i<mysql_num_rows($larequete)) {
					$champs2 = mysql_query("SHOW COLUMNS FROM $db.$latable");
					while ($row_champs2 = mysql_fetch_row($champs2)) {
						$lechamp2=$row_champs2[0];
                                                echo utf8_encode(mysql_result($larequete,$i,$lechamp2)) .";";
					}
				$i++;
				}
				echo "\n";
				}
	}
	mysql_free_result($result_tables);
 echo "\n\r";
}
?> 
  • info/mysqlphp_find_string_all_databases_all_tables_all_fields.txt
  • Dernière modification: 2018/07/18 09:46
  • par radeff