Skip to main content
Post Closed as "Duplicate" by mickmackusa php
removed deprecated tag [table] - https://meta.stackexchange.com/questions/198024/can-we-start-cleaning-up-table-tag-and-burninate-it-asap
Link
Source Link
fwoelffel
  • 401
  • 7
  • 17

PHP MySQL CREATE TABLE

I'm trying to create a table using PHP, PDO and MySQL. For the needs of my application, the name of the table has to be a variable.

Here is my code :

$request = $pdo->prepare("CREATE TABLE IF NOT EXISTS :table (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `parent_id` bigint(20) unsigned NOT NULL,
      `position` bigint(20) unsigned NOT NULL,
      `left` bigint(20) unsigned NOT NULL,
      `right` bigint(20) unsigned NOT NULL,
      `level` bigint(20) unsigned NOT NULL,
      `title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
      `type` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
      `content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;");
$request->execute(array(
    'table'=>$uuid));

Can't I use ":table" in the MySQL statement ?? Currently I wrote :

[...]
CREATE TABLE IF NOT EXISTS `$uuid`
[...]

This works but it sounds weird to me ^^' Is it the only solution to my problem ?