Location: PHPKode > scripts > Meta Scanner > meta-scanner/metascanner.php
<?
/****************************************************************************************/
/*	CLASS NAME	: MetaScanner															*/
/*	DEVELOPER	: Fryderyk Benigni														*/
/*	LICENSE		: GPL(Generic Public License)											*/
/*	DATE		: 05 November 2004														*/
/*	OBJECTIVE	: Retrieve and show Metadata from a MySql DataBase Server				*/
/*	DESCRIPTION	: This class is intended to retrieve MetaData from any MySql Server. It */
/* 	many different methods. It also offer a simple method called showAll that sows a 	*/
/*	simple table style representation of the MySql server. Anothe method very useful is */
/*	scanAll a simple method that scann the all MySql Server and store server information */
/*	about the strocture on determined variables called: $dblists(Lists of all 			*/
/*	Databases), $tblists(List of all tables indexed by the name of the relative 		*/
/*	database) and $fllists(List of all field of all the tabels indexed by the name of 	*/
/*	the tables.																			*/
/****************************************************************************************/

class MetaScanner
{
var $connection;			//holds the connection to MySql Server
var $dblists;				//Lists of all databases in a particular MySql Server
var $tblists;				//Lists of all tabels in a particular MySql Server indexed by database name
var $fllists;				//Lists of all fields in a particular MySql Server indexed by table name

/****************************************/
/*			Class Constructor			*/
/****************************************/
function __Construct($__Connection)
	{
		$this->connection = $__Connection;							//Specication and attribution os a connection
	}

/****************************************/
/*		   Connection function			*/
/****************************************/
function connect()
	{
		$this->connection = mysql_connect($this->connection);		//Connects to the specified connection
	}

/****************************************/
/*		Desconnection function			*/
/****************************************/
function disconnect()
	{
		mysql_close();
	}

/****************************************/
/*			 Return DB list    			*/
/****************************************/
function returndblist()
	{
	return $this->dblists;											
	}

/****************************************/
/*			Return Table List			*/
/****************************************/
function returntblist()
	{
	return $this->tblists;											
	}

/****************************************/
/*			Return Fields List			*/
/****************************************/
function returnfllist()
	{
	return $this->fllists;										
	}

/****************************************/
/*		   Retrieve DataBases			*/
/****************************************/
function getDataBases()
	{
		$this->dblists = mysql_query("SHOW DATABASES"); 			//Gets all DataBases of a specific connection
		return $this->dblists;										//Return the DataBases list
	}

/****************************************/
/*			Retrieve Tables 			*/
/****************************************/
function getTables($__Database)										//Calling getTables passing a specific DataBase as a parameter
	{
		mysql_select_db($__Database);								//Selecting the specific DataBase
		$this->tblists["$__Database"] = mysql_query("SHOW TABLES");	//Retrieving Table list of a specific DataBase
		return $this->tblists;										//Return List of Tables of a specific Database
	}

/****************************************/
/*			Retrieve Fields 			*/
/****************************************/
function getFields($__Database, $__Table)										//Retrieve Fields passing a Database and Table name
	{
		$this->fllists["$__Table"] = mysql_list_fields($__Database, $__Table);	//Retrieve Fields
		return $this->fllists;													//Return Fields List
	}

/****************************************/
/*		 Retrieve all MetaData			*/
/****************************************/
function scanAll()														
	{
		$this->dblists = $this->getDataBases();							//Gets Lists of DataBases in the specified MySql Server
		while ($linha =  mysql_fetch_row($this->dblists))				//Read true all the DB result
			{
			$this->tblists = $this->getTables($linha[0]);				//Gets table for each specified DataBase
			$temp = $linha[0];											//Temporary attribution of DB names
			while ($linha2 = mysql_fetch_row($this->tblists[$temp]))	//Read true all Tables
				{
				$this->fllists = $this->getFields($linha[0],$linha2[0]);//Gets all fields for the specific DB and Table
				}
			}
	}

/****************************************/
/*		 Retrieve all MetaData			*/
/****************************************/
function showAll()
	{
		$this->dblists = $this->getDataBases();							//Gets Lists of DataBases in the specified MySql Server
		while ($linha =  mysql_fetch_row($this->dblists))				//Read true all the DB result
			{
			print("<p><b><font size=\"4\" face=\"Arial\">Database : $linha[0]</font></b></p><br>");	//Print DB Name
			$this->tblists = $this->getTables($linha[0]);				//Gets table for each specified DataBase
			$temp = $linha[0];											//Temporary attribution of DB names
			while ($linha2 = mysql_fetch_row($this->tblists[$temp]))	//Read true all Tables and shows Name and FIeld Title
				{
				print("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"100%\" \"><tr>");
				print("<td width=\"100%\" colspan=\"3\" bgcolor=\"#EFEFEF\"><font face=\"Arial\"><b>Table: $linha2[0]</b></font></td>");
 				print("</tr>
					   	<tr>
						<td width=\"33%\" bgcolor=\"#D7D7D7\" align=\"center\"><b>
						<font face=\"Arial\" size=\"2\">Field Name</font></b></td>
						<td width=\"33%\" bgcolor=\"#D7D7D7\" align=\"center\"><b>
						<font face=\"Arial\" size=\"2\">Field Type</font></b></td>
						<td width=\"34%\" bgcolor=\"#D7D7D7\" align=\"center\"><b>
						<font face=\"Arial\" size=\"2\">Field Size</font></b></td>
						</tr>
						");
				$this->fllists = $this->getFields($linha[0],$linha2[0]);	//Gets all fields for the specific DB and Table
				$temp2 = $linha2[0];										//Temporary attribution of Tables names
				for($i=0;$i<mysql_num_fields($this->fllists[$temp2]);$i++)	//Shows all Fields name,type and lenght for all fields of all tables
					{
					print("<tr>
					<td width=\"33%\" align=\"center\">".mysql_field_name($this->fllists[$temp2],$i)."</td>
    				<td width=\"33%\" align=\"center\">".mysql_field_type($this->fllists[$temp2],$i)."</td>
    				<td width=\"34%\" align=\"center\">".mysql_field_len($this->fllists[$temp2],$i)."</td>
  					</tr>");
					}
				}
				print("<br>");
			}
	}
}
?> 
Return current item: Meta Scanner