Forum Moderators: coopster
Just thought i'd share this technique I just hacked together for creating an install script that must create tables in a database. Prior to distribution; use mysqldump to export the database structure to a file called database.sql that you should then include with your distribution...
mysqldump --user=username --password=password -d databasename > database.sql
...should do the trick. (-d means "no data"). It turns out that the structure of the file created by mysqldump is easily parsable; so all you need to do is create an install script that reads the file and executes the CREATE TABLE staements from it. Something like this:
InstallTables.php: (minus database connection code)
<?php
$sql = "";
$source = fopen("database.sql");
while(!feof($source))
{
// read each line from database.sql
$line = trim(fgets($source));
// if the line is not a comment (starts with --) append to $sql
if (($line) && (substr($line,0,2) <> "--")) $sql .= $line;
}
fclose($source);// explode $sql on the semi-colon characters to break out the individual queries
$queries = explode(";",$sql);// and finally execute each query
foreach($queries as $query)
{
mysql_query($query);
}
?>
Hope someone finds this handy!