Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP Sqlite
Scenario:
 
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 7:06am
amarasat1.3K
Comments
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))";
 
or
 
$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');
$query->execute();
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 (http://www.w3cyberlearnings.com/PHP_MySQL_PDO_Create_Table)

1 solution

Rate this: bad
good
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
  Permalink  
v2

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

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 3 Apr 2013
Copyright © CodeProject, 1999-2014
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