Page 1 of 1

How to feed a variable into the table creation code?

Posted: Fri Nov 14, 2003 4:22 pm
by robster

Code: Select all

$sqlcreate ='CREATE TABLE $id (
`id` INT NOT NULL AUTO_INCREMENT,
`userid` INT,
`rating` INT NOT NULL,
PRIMARY KEY ( `id` )
)';
Hi all,

Above i have a standard table creation code. What I want is where it has $id I want it to actually create the table with the variable name.

So presuming $id = "hello" the table would be created with a name of "hello".


Currently it creates a table with a litteral name of "$id".

Does anyone have an idea of how to feed a variable into the table creation code?

I'm totally stumped! This is harder than I thought!

:|

Posted: Fri Nov 14, 2003 4:35 pm
by Paddy

Code: Select all

$sqlcreate = "CREATE TABLE ".$id." ( 
`id` INT NOT NULL AUTO_INCREMENT, 
`userid` INT, 
`rating` INT NOT NULL, 
PRIMARY KEY ( `id` ) 
)";
Should work.

Posted: Fri Nov 14, 2003 4:44 pm
by robster
that worked in terms of using the concatonation . bit, but you all know what it was?!?!?

MAN!

You can't create a table that is a number.

$id was a number (id: it might have been 345 or similar).

So I added a prefix to it with $table = ("rating_"."$id"); and then created the table with a name of $table and it worked.

PHEW

Thanks a ton!

Posted: Sat Nov 15, 2003 1:49 pm
by twigletmac
The initial problem was that PHP doesn't parse stuff in single quotes:

Code: Select all

$id = 'hello';
echo '$id = '.$id;

// prints
// $id = hello
Mac