Solved

Create table where tablename is a variable

Posted on 2006-06-26
20
433 Views
Last Modified: 2008-03-10
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
Comment
Question by:cbonnock
  • 6
  • 6
  • 4
  • +1
20 Comments
 
LVL 6

Accepted Solution

by:
soapergem earned 250 total points
ID: 16988685
Use double quotes instead of single quotes.

"CREATE TABLE $tablename...
0
 
LVL 37

Assisted Solution

by:Harisha M G
Harisha M G earned 250 total points
ID: 16988687
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
 
LVL 6

Expert Comment

by:soapergem
ID: 16988690
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 37

Expert Comment

by:Harisha M G
ID: 16988694
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
 

Author Comment

by:cbonnock
ID: 16988727
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
 
LVL 6

Expert Comment

by:soapergem
ID: 16988740
What errors are you getting? And did you remember to actually assign a value to $tablename first?
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16988743
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
 

Author Comment

by:cbonnock
ID: 16988775
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16988781
cbonnock, try \@
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16988787
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16988795
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
 

Author Comment

by:cbonnock
ID: 16988804
no... same problem.
0
 
LVL 7

Expert Comment

by:maUru
ID: 16988869
you cant make a table with an @ inside it
0
 
LVL 7

Expert Comment

by:maUru
ID: 16988886
try converting the @ symbol to AT

eg:

$tablename = str_replace('@', 'AT', $tablename);
$query = "CREATE $tablename etcetc.............."
0
 

Author Comment

by:cbonnock
ID: 16988887
I worked around it... thanks to all for the quick responses.
0
 
LVL 7

Expert Comment

by:maUru
ID: 16988910
cbonnock, why the HELL did you award the points to mgh_mgharish and not soapergem?!
0
 
LVL 6

Expert Comment

by:soapergem
ID: 16988916
I was just wondering the same thing.
0
 

Author Comment

by:cbonnock
ID: 16989229
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
 

Author Comment

by:cbonnock
ID: 16991426
Thanks again.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question