1
嘗試使用PDO時CREATE TABLE
時出現相當直接的錯誤。下面的相同查詢在使用標準mysqli函數發佈時工作正常。使用PDO創建表時,「基表或視圖已存在」
請忽略編碼風格問題,這是我遇到的問題的一個粗略工作示例。 此示例的任何部分都不應用於生產環境。
$debug = true;
$ftime = date("Ymd_His_a");
$table = 'tbl_' . $ftime;
$logfile = 'debug_logfile.txt';
$db = 'somedb';
$driver = "mysqli";
$dbhost = "localhost";
$dbuser = "someuser";
$dbpass = "somepassword";
$logentry = "Initializing log file at line " . __LINE__ . '. Time: ' . $ftime . "\n";
file_put_contents($logfile, $logentry, FILE_APPEND | LOCK_EX);
// establish PDO connection
try{
$dbx_pdo = new PDO("mysql:dbname=$db;host=$dbhost", $dbuser, $dbpass);
$dbx_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbx_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
// write log line
$logentry = "PDO CREATED $_SERVER[PHP_SELF] from line " . __LINE__ . '. Time: ' . $ftime . "\n";
file_put_contents($logfile, $logentry, FILE_APPEND | LOCK_EX);
} catch(PDOException $ex) {
die(json_encode(array('outcome' => false, 'message' => 'Unable to connect')));
exit;
}
/////////////////////////////////////////////////
//
// CREATE TABLE
// first, if the table happens to exist already- drop it.
try
{
$q = "DROP TABLE IF EXISTS $table";
if ($debug) {
echo "<pre>$q</pre> at line " . __LINE__;
$logentry = "$_SERVER[PHP_SELF] from line " . __LINE__ . '. Time: ' . $ftime . '. QUERY: ' . $q . "\n";
file_put_contents($logfile, $logentry, FILE_APPEND | LOCK_EX);
}
$stmt = $dbx_pdo->query($q);
$stmt->execute();
$stmt->closeCursor();
} catch(PDOException $err) {
echo "<p>$q<br>ERROR: " . $err->getMessage();
exit;
}
// now try to create the table
try
{
$q = "
CREATE TABLE $table
(
`id` INT(10) NULL ,
`code` VARCHAR(20) NULL ,
`description` VARCHAR(12) NULL
)
ENGINE = MYISAM;
";
if ($debug) {
echo "<pre>$q</pre> at line " . __LINE__;
$logentry = "$_SERVER[PHP_SELF] from line " . __LINE__ . '. Time: ' . $ftime . '. QUERY: ' . $q . "\n";
file_put_contents($logfile, $logentry, FILE_APPEND | LOCK_EX);
}
$stmt = $dbx_pdo->query($q);
$stmt->execute();
$stmt->closeCursor();
} catch(PDOException $err) {
$logentry = "Error near line " . __LINE__ . '. Time: ' . $ftime . '. ERROR: ' . $err->getMessage() . "\n";
file_put_contents($logfile, $logentry, FILE_APPEND | LOCK_EX);
echo "<p>$q<br>ERROR: " . $err->getMessage();
}
//
// CREATE TABLE
/////////////////////////////////////////////////
以前運行腳本,我保證logfile.txt
不存在。
腳本運行之後,日誌文件logfile.txt
包含以下內容:
Initializing log file at line 10. Time: 20150120_135803_pm
PDO CREATED /program/_test2.php from line 27. Time: 20150120_135803_pm
/program/_test2.php from line 47. Time: 20150120_135803_pm. QUERY: DROP TABLE IF EXISTS tbl_20150120_135803_pm
/program/_test2.php from line 73. Time: 20150120_135803_pm. QUERY:
CREATE TABLE tbl_20150120_135803_pm
(
`id` INT(10) NULL ,
`code` VARCHAR(20) NULL ,
`description` VARCHAR(12) NULL
)
ENGINE = MYISAM;
Error near line 83. Time: 20150120_135803_pm. ERROR: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'tbl_20150120_135803_pm' already exists
腳本沒有被重新加載或稱爲第二時間,而不是嵌入在一個框架。這是香草PHP,你看到的是你得到的。日誌寫入(設置爲追加模式)是爲了確保腳本不會以某種方式第二次重新加載。
我已經檢查過,確保在運行此腳本之前,在數據庫中沒有任何表以tbl_
開頭。表不是預先存在。
後腳本運行(儘管PDO的錯誤消息)的表格顯示了(但只有後腳本運行 - 在此之前,我已經三重檢查)。
這是怎麼回事?
確認固定- 謝謝。 – 2015-01-20 19:19:09