简体   繁体   English

致命错误:具有2个查询的未捕获异常'PDOException'

[英]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. 我有2条SQL查询,似乎需要单独运行。 Query #1 throws the data into a temp (t) table. 查询1将数据扔到temp(t)表中。 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. 然后,第二个查询开始,并从(t)中获取数据,并将其与目标表中的email_addr进行比较,并&&在list_no_email.email_addr中检查以确保该记录也不存在。 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 然后第三

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:未捕获的异常“PDOException” - Fatal Error: Uncaught exception 'PDOException' PHP得到以下错误:“致命错误:未捕获的异常&#39;PDOException&#39;” - PHP Getting the following error: “Fatal error: Uncaught exception 'PDOException' ” 致命错误:未捕获的异常 &#39;PDOException&#39;,消息为 &#39;SQLSTATE[42000]: - Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: PHP致命错误:消息为&#39;SQLSTATE [42000]的未捕获异常&#39;PDOException&#39; - PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000] 致命错误:未捕获异常“PDOException”,消息“找不到驱动程序” - Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' 加载的数据: <br /> 致命错误 :消息未捕获的异常“ PDOException” - Data Loaded: <br /> <b>Fatal error</b>: Uncaught exception 'PDOException' with message PHP致命错误:消息为&#39;SQLSTATE [42000]的未捕获异常&#39;PDOException&#39;: - PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Php 致命错误未捕获的 PDOException - Php fatal error Uncaught PDOException 致命错误:未捕获的 PDOException:获取错误的值 - Fatal error: Uncaught PDOException: get wrong values 致命错误:未捕获的 PDOException:删除中的 SQLSTATE[42000] - Fatal error: Uncaught PDOException: SQLSTATE[42000] in DELETE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM