/ Published in: PHP
Sometimes it can be useful to have a dump of the current database schema. The script below reads the schema from a MySQL database and outputs XML that describes the schema.
At first we connect to a MySQL database and use the SHOW TABLES command to return all the tables in the database. Next, we iterate over each table and return the fields for each table using the SHOW FIELDS command. Finally, we put all of the returned information into XML.
You should note that this code is specific to MySQL database. The commands such as SHOW TABLES and SHOW FIELDS are also available for other databases but are specified slightly differently.
At first we connect to a MySQL database and use the SHOW TABLES command to return all the tables in the database. Next, we iterate over each table and return the fields for each table using the SHOW FIELDS command. Finally, we put all of the returned information into XML.
You should note that this code is specific to MySQL database. The commands such as SHOW TABLES and SHOW FIELDS are also available for other databases but are specified slightly differently.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php // database constants // make sure the information is correct // connection to the database // select a database to work with // return all available tables $tables[] = $row[0]; } $output = "<?xml version=\"1.0\" ?>\n"; $output .= "<schema>"; // iterate over each table and return the fields for each table foreach ( $tables as $table ) { $output .= "<table name=\"$table\">"; $output .= "<field name=\"$row1[0]\" type=\"$row1[1]\""; $output .= ($row1[3] == "PRI") ? " primary_key=\"yes\" />" : " />"; } $output .= "</table>"; } $output .= "</schema>"; // tell the browser what kind of file is come in // print out XML that describes the schema echo $output; // close the connection ?>