Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create mysql table with $variables

Status
Not open for further replies.
May 13, 2005
56
US
I was wondering if it is possible to use variables in a sql statement when creating MySQL tables..

For example, if I wanted to add a table called "good" to my database, this would be it..

Code:
$sql = 'CREATE TABLE `good`(
        `id` int( 5 ) NOT NULL AUTO_INCREMENT ,
        `good` int( 4 ) NOT NULL,
        PRIMARY KEY ( `id` ) ,
        ) TYPE = MYISAM ';

well, what if I wanted the table to be dynamic, based on user input?

Code:
$good="`good`";
$sql = 'CREATE TABLE $good (
        `id` int( 5 ) NOT NULL AUTO_INCREMENT ,
        `good` int( 4 ) NOT NULL,
        PRIMARY KEY ( `id` ) ,
        ) TYPE = MYISAM ';

I thought that would work and I have tried other variants, but cannot get it to work... Any ideas?
 
Have you tried:

Code:
$good="'good'";
$sql = [red]"[/red] CREATE TABLE " [blue].[/blue]  $good [blue]. [/blue] "(
        'id' int( 5 ) NOT NULL AUTO_INCREMENT ,
        'good' int( 4 ) NOT NULL,
        PRIMARY KEY ( 'id' ) ,
        ) TYPE = MYISAM [red]"[/red];

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
There is absolutely no reason why you can use variables to dynamically construct a CREATE TABLE query. You're just passing a string to the server.

The problem is you're using singlequotes to delineate your query string. As is documented in this PHP Online manual entry, variables are not dereferenced inside singlequoted strings.

Even if variables were deferenced inside singlequoted strings, your snippet will generate a parse error because you're using singlequotes inside a singlequoted string. How is PHP supposed to know which doublequote is an internal character and which is supposed to delineate the string unless you tell it?

Also, your snippet uses backticks in this line:

$good="`good`";

backticks have a very special use in PHP: as is talked about on this page of the PHP online manual, backticks perform external execution of commands.

Try:

Code:
$good='good';
$sql = "CREATE TABLE $good (
        `id` int( 5 ) NOT NULL AUTO_INCREMENT ,
        `good` int( 4 ) NOT NULL,
        PRIMARY KEY ( `id` ) ,
        ) TYPE = MYISAM ";

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I must have messed up my syntax, both examples that were posted worked for me.. Thank you both for your help and for the links...

Thanks,

Mike
 
Glad you sorted it out. When having probelms like that echoing your vaiables often helps determine why something is not working.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top