Forum Moderators: coopster
I need to create a table with a $ variable in the table name. The code below does not work. Could someone point me in the right direction..
TIA
-- begin code --
<?php
$table = 'test';
$SqlInsertQuery = "CREATE TABLE {$table} ('
. ' `ID` int(11) NOT NULL auto_increment,'
. ' `a_ID` int(11) NOT NULL,'
. ' `title` varchar(255) NOT NULL,'
. ' PRIMARY KEY (`ID`)'
. ' ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT= 0 ;";
?>
$table = "test";
$query = "CREATE TABLE ".$table." (`ID` int(11) NOT NULL auto_increment,`a_ID` int(11) NOT NULL,`title` varchar(255) NOT NULL,PRIMARY KEY (`ID`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0";
[b]//note that I do not have a semi-colon at the end of the query inside the string[/b]
$result = mysql_query($query) or die(mysql_error());
if($result) {
echo 'Table created!';
} else {
echo 'Table died!';
}
A couple things to remember:
*Make sure that the database user has permissions to create tables.
*Make sure that you clean the $table variable if it can be user-defined in any way.
Good luck! :)