note 27734 added to function.mysql-list-tables

From: Date: Tue, 17 Dec 2002 10:36:55 +0000
Subject: note 27734 added to function.mysql-list-tables
Groups: php.notes 
Request: Send a blank email to php-notes+get-41006@lists.php.net to get a copy of this message
I was in need of a way to create a database, complete with tables from a .sql file. Well, since PHP/mySQL doesn't allow that it seems, the next best idea was to create an empty template database and 'clone & rename it'. Guess what? There is no mysql_clone_db() function or any SQL 'CREATE DATABASE USING TEMPLATEDB' command. grrr... So, this is the hack solution I came up with: $V2DB = "V2_SL".$CompanyID; $result = mysql_create_db($V2DB, $linkI); if (!$result) $errorstring .= "Error creating ".$V2DB." database<BR>\n".mysql_errno($linkI).": ".mysql_error($linkI)."<BR>\n"; mysql_select_db ($V2DB, $linkI) or die ("Could not select ".$V2DB." Database"); //You must have already created the "V2_Template" database. //This will make a clone of it, including data. $tableResult = mysql_list_tables ("V2_Template"); while ($row = mysql_fetch_row($tableResult)) { $tsql = "CREATE TABLE ".$V2DB.".".$row[0]." AS SELECT * FROM V2_Template.".$row[0]; echo $tsql."<BR>\n"; $tresult = mysql_query($tsql,$linkI); if (!$tresult) $errorstring .= "Error creating ".$V2DB.".".$row[0]." table<BR>\n".mysql_errno($linkI).": ".mysql_error($linkI)."<BR>\n"; } -- http://www.php.net/manual/en/function.mysql-list-tables.php http://master.php.net/manage/user-notes.php?action=edit+27734 http://master.php.net/manage/user-notes.php?action=delete+27734 http://master.php.net/manage/user-notes.php?action=reject+27734

« previous php.notes (#41006) next »