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 
mysqli_connect("HOST""USR""PWD""test" ) or die( mysqli_connect_error() );
$res mysqli_query$conn"SHOW TABLES" );
echo 
"<pre>";
while( list( 
$tbl ) = mysqli_fetch_array$res )) {
  
$res2 mysqli_query$conn"show create table $tbl) or die( mysqli_error($conn) );
  while( list( 
$tbl_crt$createtxt ) = mysqli_fetch_array$res2 )) {
    echo 
"-- Table info for $tbl_crt\n$createtxt\n\n";
  }
}
echo 
"</pre>";
?>



Return to the Artful MySQL Tips page