Jump to content

trouble creating table


Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.