3

I'm trying to create a new table with this code:

try {
    $db = new PDO("mysql:hostname=localhost",'root','root');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo $e->getMessage();
    die();
}

//to create database
$db->query("CREATE DATABASE theShop IF NOT EXISTS ;");
$db->query("USE theShop;");

$createTableShops = "CREATE TABLE `advertisor`
                        (
                            `ShopID`        UNSIGNED INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                            `ShopName`      VARCHAR(50) NOT NULL,           
                        )type=InnoDB;";

try {
    $db->query($createTableShops);
} catch(PDOException $e) {
    echo $e->getMessage();
}

at phpMyAdmin I browse for the database to make sure if it's actually have been created, I tried the code over and over but it always creates the database theShop but with 0(Zero) tables, why the table not created?

note: I've tried some changes:

  1. not using backtick
  2. changing the type of table
  3. changing the key word type to engine
  4. not using neither type nor engine
  5. changing the connection string by adding dbname=theShop
  6. many things ..!
Achrome
  • 7,773
  • 14
  • 36
  • 45
Poula Adel
  • 609
  • 1
  • 10
  • 33
  • the ',' character after `ShopName` field is not in my code, a fault copying the code nothing more. – Poula Adel Jun 16 '15 at 08:50
  • Have you tried this : http://stackoverflow.com/questions/19577056/using-pdo-to-create-table ? – tmylamoule Jun 16 '15 at 08:51
  • yes yes yes!... I've searched alot before asking! – Poula Adel Jun 16 '15 at 08:52
  • So it does not create the table but it also doesn't output any error...? Have you checked whether `query()` returns `true`? Are you 200% sure you're looking at the right database? – deceze Jun 16 '15 at 08:57
  • Yes I've checked it .. I've tried to do some output if the query is not done correctly . It also doesn't give any output ... I make an output after all at end of code and the output appears! but the query testing doesn't – Poula Adel Jun 16 '15 at 09:00

1 Answers1

0

Try this (works on my computer : Ubuntu, Apache2, MySQL)

<?php
try {
    $db = new PDO("mysql:hostname=localhost",'root','pw');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo $e->getMessage();
    die();
}

//to create database
$db->query("CREATE DATABASE IF NOT EXISTS  theShop;");
$db->query("USE theShop;");

$createTableShops = "CREATE TABLE `advertisor`
                        (
                            `ShopID`        INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
                            `ShopName`      VARCHAR(50) NOT NULL           
                        )ENGINE=InnoDB";

try {
    $db->query($createTableShops);
} catch(PDOException $e) {
    echo $e->getMessage();
}
?>

Let me know if it works

tmylamoule
  • 161
  • 1
  • 12
  • on Windows 8 , AppServ -> It Works! thx! ... but what was the fault in my code ? .you've changed `UNSIGNED INT` to `BIGINT` only – Poula Adel Jun 16 '15 at 09:10
  • 1
    If you want to use UNSIGNED : go for `'ShopID' INT UNSIGNED` not `'ShopID' UNSIGNED INT` Also, keep in mind you can debug php with console http://php.net/manual/en/install.windows.commandline.php – tmylamoule Jun 16 '15 at 09:18
  • 1
    Yeah I've only changed type to engine and UNSIGNED INT to BIGINT – tmylamoule Jun 16 '15 at 09:25