0

我很确定我知道这个问题,我只是不知道如何让自己出去。我有 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
4

1 回答 1

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));

然后是第三个

于 2013-09-07T16:48:53.983 回答