I have been struggling since this morning, trying to write an install.php file that will insert three empty tables (users, sessions, posts) into a database. The SQL code I'm using is valid when I inject it with PHPMyAdmin, but apparently the way I'm handling it in PHP is wrong, because when I run my install.php file my database remains empty. Here's my code:
<?php
try {
$con = new PDO('mysql:host=omitted;dbname=omitted','omitted','');
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$q="CREATE TABLE posts (
title varchar(150) NOT NULL,
body text NOT NULL,
created varchar(100) NOT NULL,
user varchar(40) NOT NULL,
id int(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
UNIQUE KEY id (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE sessions (
session_id varchar(40) NOT NULL,
data text NOT NULL,
last_activity int(11) NOT NULL,
PRIMARY KEY (session_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(40) NOT NULL,
password varbinary(250) NOT NULL,
email varchar(40) NOT NULL,
salt varchar(20) NOT NULL,
name varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (id),
UNIQUE KEY email (email),
UNIQUE KEY username (username),
UNIQUE KEY id (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ";
$stmt = $con->prepare($q);
$stmt->execute();
echo "success";
} catch (PDOException $e) {
$e->getMessage();
}
?>
I am assuming that it's some idiotic mistake on my part (since my last 3 headaches were also quite silly) but at this moment I really can't figure it out. Any ideas?