Click here to Skip to main content
11,796,393 members (70,746 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: PHP Sqlite

TO crate a table we use the query as shown below

$query = 'CREATE TABLE xyz ' .
			'(Id varchar(30), Val varchar(30))';

Here the table name is "xyz". I know the table name so i was able to create it. In the next case i do not know the name of the table and the name is in the variable $TableName. How do i create the table now

$TableName = "xyz"; -->Calculated in run time
$query = 'CREATE TABLE '.$TableName.' ' .
			'(Id varchar(30), Val varchar(30))';

When i execute the table is not being created. Is this format correct?
Posted 3-Apr-13 6:06am
enhzflep at 3-Apr-13 13:05pm
See here: Can Php Pdo statements accept the table name as parameter?

Yes, the format is correct.
Summary: Can't bind either a column name or a table name. You have to use string concatenation.
Prasad Khandekar at 3-Apr-13 17:02pm
I am sure he is using string concatenation. ($query = 'CREATE TABLE ' . $TableName . ' ' . '(Id varchar(30), Val varchar(30))';
enhzflep at 3-Apr-13 17:44pm
Me too. It's an odd question - examples of string concatenation yet nothing that is specific to pdo (at least, so it seems).
Even the strings that are concatenated could simply be written in a single expression.
For instance, I still cannot work out why one would write this

$query = 'CREATE TABLE xyz ' .
'(Id varchar(30), Val varchar(30))';

instead of

$tblName = 'someName';
$query = "CREATE TABLE $tblName (Id varchar(30), Val varchar(30))";


$tblName = 'someName';
$query = 'CREATE TABLE ' . $tblName . ' (Id varchar(30), Val varchar(30))';

or even

$tblName = 'someName';
$query = sprintf('CREATE TABLE %s (Id varchar(30), Val varchar(30))', $tblName);

I'd naturally have expected to see something like the following code. That is until I realized that prepared statements are but one of the advantages of Pdo - the other being the fact it works with a bunch of different DBs, i.e sqLite.

$params = array(':tableName' => 'xyz');
$query = $pdo->prepare('select * from :tableName');
Prasad Khandekar at 3-Apr-13 17:11pm
Hello Amarsat,

The problem might be in the table name generation. Have you tried running the second snippet in which the $TableName is set to value "xyz". Please also have a look at (

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

$query = "CREATE TABLE $TableName".
                 '(Id varchar(30), Val varchar(30))';

This has worked fine. Thanks for all your support enhzflep and Prasad

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 530
1 Abhinav S 392
2 OriginalGriff 340
3 CPallini 269
4 KrunalRohit 249
0 OriginalGriff 2,012
1 Maciej Los 1,725
2 KrunalRohit 1,310
3 CPallini 1,055
4 ppolymorphe 858

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 3 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100