Skip to content

Instantly share code, notes, and snippets.

@sndsgd sndsgd/PDO-bug.php
Last active Aug 29, 2015

Embed
What would you like to do?
PDO bug
<?php
const DB_HOST = 'localhost';
const DB_NAME = 'php_bug_test';
const DB_TABLE = 'timestamp_test';
const DB_USER = 'root';
const DB_PASSWORD = 'root';
$dbSQL = "CREATE DATABASE IF NOT EXISTS ".DB_NAME;
$combo = DB_NAME.'.'.DB_TABLE;
$tableSQL = <<<TABLE
CREATE TABLE IF NOT EXISTS $combo (
id int(11) NOT NULL AUTO_INCREMENT,
value varchar(255) NOT NULL,
dateAdded timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY ("id")
);
TABLE;
try {
$dsn = "mysql:host=".DB_HOST;
$db = new PDO($dsn, DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => true
]);
$dbCreateResult = $db->query($dbSQL);
$tableCreateResult = $db->query($tableSQL);
$query = $db->query("SELECT COUNT(*) FROM $combo");
if ($query->fetchColumn() == 0) {
$db->query("INSERT INTO $combo (value) VALUES ('one')");
}
echo "PDO::ATTR_EMULATE_PREPARES = true:\n";
$query = $db->query("SELECT dateAdded FROM $combo");
echo $query->fetchColumn()."\n";
$db = null;
$db = new PDO($dsn, DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false
]);
echo "PDO::ATTR_EMULATE_PREPARES = false:\n";
$query = $db->query("SELECT dateAdded FROM $combo");
echo $query->fetchColumn()."\n";
}
catch (PDOException $ex) {
die($ex->getMessage()."\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.