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 ..!
5
  • the ',' character after ShopName field is not in my code, a fault copying the code nothing more.
    – Poula Adel
    Commented Jun 16, 2015 at 8:50
  • Have you tried this : stackoverflow.com/questions/19577056/using-pdo-to-create-table ?
    – tmylamoule
    Commented Jun 16, 2015 at 8:51
  • yes yes yes!... I've searched alot before asking!
    – Poula Adel
    Commented Jun 16, 2015 at 8: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
    Commented Jun 16, 2015 at 8: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
    Commented Jun 16, 2015 at 9:00

1 Answer 1

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

3
  • 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
    Commented Jun 16, 2015 at 9: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 php.net/manual/en/install.windows.commandline.php
    – tmylamoule
    Commented Jun 16, 2015 at 9:18
  • 1
    Yeah I've only changed type to engine and UNSIGNED INT to BIGINT
    – tmylamoule
    Commented Jun 16, 2015 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.