amplexus Posted August 27, 2010 Share Posted August 27, 2010 Hi, I'm trying to use a variable- "$newdbname" in a script that sets up a new database. I'm using this variable because I have an include file that sets all login info and the database name as variables. here's the code. or at lest the relevant part. <?php require 'dbinfo.inc.php'; $db = mysql_connect($servname,$dbusername,$dbpassword) or die ('Unable to connect. Check your connection parameters.'); mysql_select_db($database, $db) or die(mysql_error($db)); // create the user table $query = 'CREATE TABLE ($newdbname)( user_id int(30) NOT NULL AUTO_INCREMENT, username varchar(20) NOT NULL, password varchar(41) NOT NULL, PRIMARY KEY (user_id) ) ENGINE=MyISAM'; mysql_query($query, $db) or die (mysql_error($db)); if I run the script like this, I get the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '($newdbname)( user_id int(30) NOT NULL AUTO_INCREMENT, usernam' at line 1 if I run the code as this, without parentheses around the variable, it creates a table named "$newdbname" <?php require 'dbinfo.inc.php'; $db = mysql_connect($servname,$dbusername,$dbpassword) or die ('Unable to connect. Check your connection parameters.'); mysql_select_db($database, $db) or die(mysql_error($db)); // create the user table $query = 'CREATE TABLE $newdbname ( user_id int(30) NOT NULL AUTO_INCREMENT, username varchar(20) NOT NULL, password varchar(41) NOT NULL, PRIMARY KEY (user_id) ) ENGINE=MyISAM'; mysql_query($query, $db) or die (mysql_error($db)); what am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/211844-using-a-variable-to-name-a-database/ Share on other sites More sharing options...
Alex Posted August 27, 2010 Share Posted August 27, 2010 You need to use double quotes because single quotes do not have variable interpolation. And just to nitpick, you're creating a table, not a database. $query = "CREATE TABLE $newdbname ( user_id int(30) NOT NULL AUTO_INCREMENT, username varchar(20) NOT NULL, password varchar(41) NOT NULL, PRIMARY KEY (user_id) ) ENGINE=MyISAM"; Quote Link to comment https://forums.phpfreaks.com/topic/211844-using-a-variable-to-name-a-database/#findComment-1104229 Share on other sites More sharing options...
Vince889 Posted August 27, 2010 Share Posted August 27, 2010 Use double quotes instead of single-quotes. Then, encapsulate your $newdbname variable with single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/211844-using-a-variable-to-name-a-database/#findComment-1104231 Share on other sites More sharing options...
amplexus Posted August 27, 2010 Author Share Posted August 27, 2010 okay, so that got me through part of it, but now I get this: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users1'( user_id int(30) NOT NULL AUTO_INCREMENT, username va' at line 1 The "users1" part is the string from the variable, thanks for that much so far! what else is going wrong? what syntax is off? Quote Link to comment https://forums.phpfreaks.com/topic/211844-using-a-variable-to-name-a-database/#findComment-1104233 Share on other sites More sharing options...
trq Posted August 27, 2010 Share Posted August 27, 2010 Do not surround $newdbname in single quotes as advised by Vince889, post your code if the problem persists. Quote Link to comment https://forums.phpfreaks.com/topic/211844-using-a-variable-to-name-a-database/#findComment-1104235 Share on other sites More sharing options...
amplexus Posted August 27, 2010 Author Share Posted August 27, 2010 removed quotes as advised. received following message. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(users234) (user_id, username, password) VALUES (1, "23",' at line 1 which seems to be referring to teh second part of my code: <?php require 'dbinfo.inc.php'; $db = mysql_connect($servname,$dbusername,$dbpassword) or die ('Unable to connect. Check your connection parameters.'); mysql_select_db($database, $db) or die(mysql_error($db)); // create the user table $query = "CREATE TABLE $newdbname( user_id int(30) NOT NULL AUTO_INCREMENT, username varchar(20) NOT NULL, password varchar(41) NOT NULL, PRIMARY KEY (user_id) ) ENGINE=MyISAM"; mysql_query($query, $db) or die (mysql_error($db)); $query = "INSERT IGNORE INTO ($newdbname) (user_id, username, password) VALUES (1, '23', PASSWORD('23')), (2, 'amplexus', PASSWORD('password'))"; mysql_query($query, $db) or die (mysql_error($db)); echo 'Success!'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/211844-using-a-variable-to-name-a-database/#findComment-1104239 Share on other sites More sharing options...
trq Posted August 27, 2010 Share Posted August 27, 2010 Remove the () around your table name. Quote Link to comment https://forums.phpfreaks.com/topic/211844-using-a-variable-to-name-a-database/#findComment-1104244 Share on other sites More sharing options...
amplexus Posted August 27, 2010 Author Share Posted August 27, 2010 echo "Success!"; thanks! Quote Link to comment https://forums.phpfreaks.com/topic/211844-using-a-variable-to-name-a-database/#findComment-1104249 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.