"基表或视图已存在"使用PDO创建表时

时间:2015-01-20 19:11:47

标签: php pdo mysqli

尝试使用PDO CREATE TABLE时,我收到了相当简单的错误。当发布标准的mysqli函数时,下面的相同查询工作正常。

请忽略编码风格问题,这是我遇到的问题的粗略工作示例。 此示例的任何部分都不应在生产环境中使用。

$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错误消息)表格显示(但> 脚本运行后 - 而不是之前,因为我已经检查过三次)。

这里发生了什么?

1 个答案:

答案 0 :(得分:2)

您正在执行两次查询,首先准备语句时只需要execute(),而不是直接使用query()运行查询:

$stmt = $dbx_pdo->query($q);
$stmt->execute();

应该是:

$stmt = $dbx_pdo->query($q);