[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

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.
0
cbonnock
Asked:
cbonnock
  • 6
  • 6
  • 4
  • +1
2 Solutions
 
soapergemCommented:
Use double quotes instead of single quotes.

"CREATE TABLE $tablename...
0
 
Harisha M GCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now