Create table where tablename is a variable

I am trying to create a table where the tablename is a variable. When I do, it creates a table with the variable (it will look like $tablename) instead of the value that $tablename represents.

Example code:
$mysql = 'CREATE TABLE $tablename (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, col1 VARCHAR( 150 ) NOT NULL, col2 VARCHAR( 150 ) NOT NULL)';

I am sure the variable is working correctly.

I need this ASAP... thanks.
cbonnockAsked:
Who is Participating?
 
soapergemConnect With a Mentor Commented:
Use double quotes instead of single quotes.

"CREATE TABLE $tablename...
0
 
Harisha M GConnect With a Mentor Commented:
Hi, try changing single quote to double quote  !

$mysql = "CREATE TABLE $tablename (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, col1 VARCHAR( 150 ) NOT NULL, col2 VARCHAR( 150 ) NOT NULL)";


---
Harish
0
 
soapergemCommented:
Alternatively, you can separate the variable from the rest of the string using the concatenation operator (aka the period):

'CREATE TABLE ' . $tablename . ' (id INT...
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Harisha M GCommented:
cbonnock, the main difference between single and double quotes is that in the double quotes, variable substitution takes place, while in single quotes, it doesn't.

So, if you have

$str = "ABC";

$text1 = '$str';
$text2 = "$str";

text1 will have $str, and text2 will have ABC.
0
 
cbonnockAuthor Commented:
I have tried all of the following combinations with no improvement:

$mysql = 'CREATE TABLE $tablename (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, col1 VARCHAR( 150 ) NOT NULL, col2 VARCHAR( 150 ) NOT NULL)';

$mysql = "CREATE TABLE $tablename (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, col1 VARCHAR( 150 ) NOT NULL, col2 VARCHAR( 150 ) NOT NULL)";

$mysql = 'CREATE TABLE '.$tablename.' (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, col1 VARCHAR( 150 ) NOT NULL, col2 VARCHAR( 150 ) NOT NULL)';

$mysql = "CREATE TABLE ".$tablename." (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, col1 VARCHAR( 150 ) NOT NULL, col2 VARCHAR( 150 ) NOT NULL)";

$mysql = 'CREATE TABLE ".$tablename." (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, col1 VARCHAR( 150 ) NOT NULL, col2 VARCHAR( 150 ) NOT NULL)';

What else might work?
0
 
soapergemCommented:
What errors are you getting? And did you remember to actually assign a value to $tablename first?
0
 
Harisha M GCommented:
cbonnock, before executing the query, echo it..

$tablename = "something";

$mysql = "CREATE TABLE $tablename (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, col1 VARCHAR( 150 ) NOT NULL, col2 VARCHAR( 150 ) NOT NULL)";

echo $mysql;

mysql_query($mysql) or die(mysql_error());
0
 
cbonnockAuthor Commented:
OK... now I see the problem... the result of the variable is an email address... the @ symbol is giving the sql query a problem... is there a way I can still use the email with the @ as a table name? Otherwise it works fine with just double quotes around the whole thing.
0
 
Harisha M GCommented:
cbonnock, try \@
0
 
Harisha M GCommented:
cbonnock, or a better and prefered way is to use...

$tablename = "something";

$mysql = "CREATE TABLE $tablename (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, col1 VARCHAR( 150 ) NOT NULL, col2 VARCHAR( 150 ) NOT NULL)";

mysql_query(mysql_real_escape_string($mysql)) or die(mysql_error());
0
 
Harisha M GCommented:
Or you may need to put the optional quotes for the table and field names...


$mysql = "CREATE TABLE `$tablename` (`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT, `col1` VARCHAR( 150 ) NOT NULL, `col2` VARCHAR( 150 ) NOT NULL)";
0
 
cbonnockAuthor Commented:
no... same problem.
0
 
maUruCommented:
you cant make a table with an @ inside it
0
 
maUruCommented:
try converting the @ symbol to AT

eg:

$tablename = str_replace('@', 'AT', $tablename);
$query = "CREATE $tablename etcetc.............."
0
 
cbonnockAuthor Commented:
I worked around it... thanks to all for the quick responses.
0
 
maUruCommented:
cbonnock, why the HELL did you award the points to mgh_mgharish and not soapergem?!
0
 
soapergemCommented:
I was just wondering the same thing.
0
 
cbonnockAuthor Commented:
My apologies... with all of the other questions I had, I didn't even realize you said the same thing... i would have split the points had I noticed... is there anything i can do about that now?
0
 
cbonnockAuthor Commented:
Thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.