#! /usr/bin/php
<?php
error_reporting(63);
/***************************************
** Title........: list all databases, all tables, all fields from a mysql server
** Filename.....: mysqlphp_list_all_databases_all_tables_all_fields.php
** Author.......: Fred Radeff
** Version......: 0.0
** Notes........:
** Last changed.: 09/24/2008
** Last change..: --- //[[fradeff@akademia.ch|Fred Radeff]] 2008/09/24 15:35//
***************************************
############ 
# 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: ";
 echo $db;
$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: ";
	$latable=$rowtables[0];
	echo "{$rowtables[0]}\n";
 
		#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: ";
			echo "{$row_champs[0]}\n";
		}
		}
	}
	mysql_free_result($result_tables);
 
?>

connect.inc.php

<?php
###################################
# variables and connection with MySQL DB
###################################

$login="root";                     // user name for you database
$pass="xxxx";                                // password to the database
 
 
function connect_db()
        {
        global $pass,$login;
        $db=mysql_connect("localhost",$login,$pass);   
        return $db;
        }
function db_name()
        {
        global $database_name;
        $db_name=$database_name;
        return $db_name;
        }
?>
  • info/mysqlphp_list_all_databases_all_tables_all_fields.txt
  • Dernière modification: 2018/07/18 09:46
  • par radeff