Solved

Create table where tablename is a variable

Posted on 2006-06-26
20
430 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now