简体   繁体   中英

Fatal error: Uncaught exception 'PDOException' with 2 queries

I am pretty sure I know the issue, I just don't know how to get myself out. I have 2 sql queries that appear to need to be run separately. Query #1 throws the data into a temp (t) table. Then the second query kicks in and take the data from (t) and compares it to email_addr in the target table && checks in list_no_email.email_addr to make sure the record isn't there either. The error smells of conflicting queries.Just not sure of another way to do it.

<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

Do not run multiple queries in one call

Run the first one

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

then go for the second

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

then for the third

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM