Solved

Create table where tablename is a variable

Posted on 2006-06-26
20
429 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 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

17 Experts available now in Live!

Get 1:1 Help Now