Create Table script

from the Artful MySQL Tips List


If your database is on a hosted provider, you may not have access to mysqldump. Here is simple PHP code to generate CREATE TABLE statements for a database. Fill in your host name, user name, password and database name:

<?php
$conn 
mysql_connect("HOST""USR""PWD") or die( mysql_error() );
$db 'test';
mysql_select_db"DBNAME"$conn ) or die( mysql_error() );
$res mysql_query"SHOW TABLES"$conn );
echo 
"<pre>";
while( list( 
$tbl ) = mysql_fetch_array$res )) {
  
$res2 mysql_query"show create table $tbl"$conn ) or die( mysql_error() );
  while( list( 
$tbl_crt$createtxt ) = mysql_fetch_array$res2 )) {
    echo 
"-- Table info for $tbl_crt\n$createtxt\n\n";
  }
}
echo 
"</pre>";
?>



Return to the Artful MySQL Tips page