How to feed a variable into the table creation code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

How to feed a variable into the table creation code?

Post 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!

:|
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post 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.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post 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!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply