matthewchristianson Posted July 5, 2007 Share Posted July 5, 2007 There is much more to this script, but luckily that all works. The only part that doesn't work is the create table bit. When I simply name the new table such as 'local_school' it works fine and all the data is entered, but for some reason because the table anme is a variable it refuses to create the table. I have checked the variable by echoing it and it works fine. mysql_connect("localhost", "root", "******") or die(mysql_error()); mysql_select_db("school_users") or die(mysql_error()); $sql = 'CREATE TABLE $school_name ( `student_ID` INT( 3 ), `username` VARCHAR( 150 ) NOT NULL, `password` INT( 20 ) DEFAULT 0 NOT NULL, PRIMARY KEY ( `student_ID` ) );'; echo 'contacting database'; mysql_query( $sql ); mysql_query("INSERT INTO $school_name VALUES ( '$studentIDA', '$tokenA', '$usernameA' )"); mysql_query("INSERT INTO $school_name VALUES ( '$studentIDB', '$tokenB', '$usernameB' )"); mysql_query("INSERT INTO $school_name VALUES ( '$studentIDC', '$tokenC', '$usernameC' )"); mysql_query("INSERT INTO $school_name VALUES ( '$studentIDD', '$tokenD', '$usernameD' )"); mysql_query("INSERT INTO $school_name VALUES ( '$studentIDE', '$tokenE', '$usernameE' )"); ?> Any help would be welcome. Thanks alot. MC Link to comment https://forums.phpfreaks.com/topic/58584-trouble-creating-table/ Share on other sites More sharing options...
bbaker Posted July 5, 2007 Share Posted July 5, 2007 TRY changing this line: $sql = 'CREATE TABLE $school_name ( to this: $sql = 'CREATE TABLE ' . $school_name . ' ( I added spaces to the stuff I added for clarity. Link to comment https://forums.phpfreaks.com/topic/58584-trouble-creating-table/#findComment-290630 Share on other sites More sharing options...
wcsoft Posted July 5, 2007 Share Posted July 5, 2007 Yeah, the problem is your create table is in single quotes, so that entire query is going to be run as a literal, meaning it's going to try and create a table named $school_name. bbaker's suggestion should work fine, or you can change to using double quotes, so it will actually parse the variable into a real table name. Link to comment https://forums.phpfreaks.com/topic/58584-trouble-creating-table/#findComment-290662 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.