PHP connection object request - PDO not working
I am 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 go through the database to make sure it was actually created. I tried the code over and over, but it always creates theShop database , but with 0 (Zero), why is the table not created?
Note. I tried some changes:
- do not use reverse
- changing table type
- changing the keyword
type
toengine
- uses neither
type
norengine
- changing the connection string by adding
dbname=theShop
- many things..!
+3
source to share
1 answer
Try it (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
0
source to share