Library code snippets
Get metadata on MySQL databases
Getting information about databases if essential if you want to write generic and scalable applications. This code shows you how to get information such as all databases on the server, all tables in each database and all field and field info for each table. Even if you do not need to build on this code, you might want to copy the code which prints out all databases, tables and field information plus examples. Its a great way to get an overview of the tables you are working on for a project.
<?
//getting metadata on MySQL databases
//output the structure of a table
$connection_1 = mysql_connect("localhost");
$fields = mysql_list_fields("cmphp","rights");
for($i=0;$i<mysql_num_fields($fields);$i++) {
echo mysql_field_name($fields,$i)." (".mysql_field_len($fields,$i).") - ".mysql_field_type($fields,$i)."<br>";
}
mysql_close;
//show the structure of ALL tables in ALL databases on the server
$server_connection_1 = mysql_connect("localhost");
$databases = mysql_query("SHOW DATABASES");
while($database = mysql_fetch_row($databases)) {
echo '<h2>DATABASE: '.$database[0].'</h2>';
$database_connection_1 = mysql_select_db($database[0]);
$tables = mysql_query("SHOW TABLES");
while($table = mysql_fetch_row($tables)){
echo '<table border="1" cellpadding="5" width="500">';
echo '<tr><td colspan="3" bgcolor="silver">TABLE: '.$table[0].'</td></tr>';
$fields = mysql_list_fields($database[0],$table[0]);
for($i=0;$i<mysql_num_fields($fields);$i++) {
echo '<tr>';
echo '<td>'.mysql_field_name($fields,$i)."</td>";
echo '<td>'.mysql_field_len($fields,$i)."</td>";
echo '<td>'.mysql_field_type($fields,$i)."</td>";
echo '</tr>';
}
echo '</table><br>';
}
}
mysql_close;
//show the nice structure of a particular database
$server_connection_1 = mysql_connect("localhost");
$the_database = "cmphp";
echo '<h2>DATABASE: '.$the_database.'</h2>';
$database_connection_1 = mysql_select_db($the_database);
$tables = mysql_query("SHOW TABLES");
while($table = mysql_fetch_row($tables)){
echo '<table border="1" cellpadding="5" width="600">';
echo '<tr><td colspan="4" bgcolor="silver"><b>TABLE: '.$table[0].'</b></td></tr>';
echo '<tr><td bgcolor="silver">NAME</td><td bgcolor="silver">SIZE</td><td bgcolor="silver">TYPE</td><td bgcolor="silver">EXAMPLE</td></tr>';
$fields = mysql_list_fields($the_database,$table[0]);
for($i=0;$i<mysql_num_fields($fields);$i++) {
echo '<tr>';
echo '<td>'.mysql_field_name($fields,$i)."</td>";
echo '<td>'.mysql_field_len($fields,$i)."</td>";
echo '<td>'.mysql_field_type($fields,$i)."</td>";
$rows = mysql_query("SELECT ".mysql_field_name($fields,$i)." FROM ".$table[0]." LIMIT 1");
$row = mysql_fetch_array($rows);
echo '<td bgcolor="eeeeee">'.$row[0].' </td>';
echo '</tr>';
}
echo '</table><br>';
}
mysql_close;
?>
Related articles
Related discussion
-
Lets Open our Eyes
by mawcot (0 replies)
-
London PHP / MySQL Superstar needed!
by gatewaytechnolabs (1 replies)
-
PHP - Dreamweaver Web Developer Required - London UK
by webdeveloperit (3 replies)
-
how create multilanguage website in php using unicode database as mysql?
by osmancarik (2 replies)
-
New Website requires Top level PHP MYSQL Program Advisor
by raj_animator967452 (1 replies)
Related podcasts
-
Scaling Large Web Sites with Joe Stump, Lead Architect at DIGG
Have you ever wanted to learn how top 100 web sites are architected? Deep Fried Bytes hosts Keith Elder and Chris Woodruff sat down with Joe Stump, Lead Architect at DIGG to discuss scaling large web sites, his life, development experiences and team building. Listen to the showThanks to our gues...
Events coming up
-
Dec
3
The Auckland PHP December meetup
Auckland, New Zealand
Topic: Magento E-Commerce platform Speaker: Robert Popovic, LERO9, Robert is the Technical Director and co-founder of LERO9. Robert attended the Electrotechnical Faculty at The University of Belgrade where he graduated with a Masters in Computer Science and Information Technology. Robert has worked exclusively in the field of web and software development throughout his career.
I try to show my databases but I get this:
Warning: mysqlfetchrow(): supplied argument is not a valid MySQL result resource
I use:
$databases = mysqlquery("SHOW DATABASES", $link);
while($database = mysqlfetch_row($databases))
{
echo '<h2>DATABASE: '.$database[0].'</h2>';
}
??
This thread is for discussions of Get metadata on MySQL databases.