致命错误:带有2个查询的未捕获异常“PDOException”

时间:2013-09-07 09:36:42

标签: php mysql sql csv pdo

我很确定我知道这个问题,我只是不知道如何让自己出局。我有2个sql查询似乎需要单独运行。查询#1将数据抛出到temp(t)表中。然后第二个查询启动并从(t)获取数据并将其与目标表中的email_addr进行比较&&检查list_no_email.email_addr以确保记录不存在。 错误的气味是相互冲突的查询。不知道另一种方法。

<p></p>
  <form enctype="multipart/form-data" method="POST">
        <p> Give a unique name for your upload </p>
        <input name="row_name" type="text">
        <br/>
        <input name="userfile" type="file">
        <br/>
        <input type="submit" value="Upload">

  </form>
<?php
$dsn = "mysql:host=$host;port=$port;dbname=$dbname"; //Data Source Name = Mysql
$db = new PDO($dsn, $db_username, $db_password,array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); //Connect to DB
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
if (isset($_FILES['userfile'], $_POST['row_name'])){
        $csv_file_name = $_FILES['userfile']['name'];
        $csv_file = $_FILES['userfile']['tmp_name'];
        $pre_do = $db->prepare(
                        "CREATE TEMPORARY TABLE t(i int);
                         LOAD DATA INFILE $csv_file_name
                         IGNORE INTO TABLE t
                         FIELDS TERMINATED BY ','
                         ENCLOSED BY '\"'
                         LINE TERMINATED BY '\r\n'"
                        );
        // exit if file fail
        if ( ! is_file( $csv_file ) )
                exit('File not found.');

        if (($handle = fopen($csv_file, "r")) !== FALSE) {
                $pre_do->execute(fgetcsv($handle));
        }
        fclose($handle);
        $do = $db->prepare(
                "INSERT INTO list_email(list_name, fname, lname, email_addr)
                 SELECT ins.*
                 FROM(
                        SELECT t.fname AS fname,
                        t.lname AS lname,
                        t.email_addr AS email_addr
                        FROM dual
                        UNION ALL
                        SELECT list_no_email.fname, list_no_email.lname, list_no_email.email_addr
                        FROM dual
                )
                AS ins WHERE NOT EXISTS(
                        SELECT 1
                        FROM list_email AS e
                        WHERE e.email_addr = ins.email_addr
                )
                AND NOT EXISTS(
                        SELECT 1
                        FROM list_no_email AS ne
                        WHERE ne.email_addr = ins.email_addr
                )"
        );
        $do->execute();

}
exit( "Complete!" );
?>

</body>
</html>

 Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.' in /home/wemail1/www/pages/campaign-build.inc.php:69 Stack trace: #0 /home/wemail1/www/pages/campaign-build.inc.php(69): PDOStatement->execute() #1 /home/wemail1/www/index.php(170): include('/home/wemail1/w...') #2 {main} thrown in /home/wemail1/www/pages/campaign-build.inc.php on line 69

1 个答案:

答案 0 :(得分:0)

不要在一次通话中运行多个查询

运行第一个

    $db->query("CREATE TEMPORARY TABLE t(i int)");

然后去第二个

    // exit if file fail
    if ( ! is_file( $csv_file_name ) ) exit('File not found.');

    $stmt = $db->prepare("LOAD DATA INFILE ?
                     IGNORE INTO TABLE t
                     FIELDS TERMINATED BY ','
                     ENCLOSED BY '\"'
                     LINE TERMINATED BY '\r\n'"
                    );
    $stmt->execute(array($csv_file_name));

然后是第三个