Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP向sql表添加2d数组和1d数组的问题_Php_Sql_Arrays - Fatal编程技术网

PHP向sql表添加2d数组和1d数组的问题

PHP向sql表添加2d数组和1d数组的问题,php,sql,arrays,Php,Sql,Arrays,好的,我现在遇到的问题是,我不明白如何使用exec()函数将数据传递到LOAD data LOCAL infleSQL命令 在我的代码的第57行,我尝试使用内爆(',',$row)。我也尝试过使用serialize($row),首先我尝试过引用,就像下面的两行一样,我也尝试过手动给它每个参数和值,这是可行的,但不实用,因为导入的CSV文件中每行上都会有100多个条目,一定有更好的方法可以做到这一点,如果有人能告诉我怎么做,我很高兴 问题发生在以下代码的第57行: <?php /*

好的,我现在遇到的问题是,我不明白如何使用
exec()函数将数据传递到
LOAD data LOCAL infle
SQL命令

在我的代码的第57行,我尝试使用
内爆(',',$row)
。我也尝试过使用
serialize($row)
,首先我尝试过引用,就像下面的两行一样,我也尝试过手动给它每个参数和值,这是可行的,但不实用,因为导入的CSV文件中每行上都会有100多个条目,一定有更好的方法可以做到这一点,如果有人能告诉我怎么做,我很高兴

问题发生在以下代码的第57行:

<?php
    /* DISCLAIMER: I, the creator of this script am in no way responsible for any damage or data loss said script may cause directly or indirectly to the used databases, servers, clients and/or devices it may be run on. ANY and ALL responsibility for issues pertaining to the use of this script falls to the person that requested it be created. By using this script you confirm that you have read, fully understand and fully agree to this disclaimer */
    /* This script has no input sanitization which means that if someone uploads a csv with any php and possibly SQL commands too in it your server will do whatever those commands tell it to, this could range from adding a malformed entry to deleting everything in the database*/

    /* ######################################## Required Configuration 'Settings' Start ######################################## */
    /* Please only change things between the "" characters unless you know what you're doing */
    /* Add your IMAP email server credentials in the next 3 lines */
    $mailbox = "{imap.gmail.com:993/imap/ssl}INBOX"; /* This bit tells the script where to look and what folder to look for (you may need to change INBOX to the apropriate folder name) */
    $emailAddress = "the_address_to_read_emails_from"; /* The email you wish to process emails from */
    $emailPassword = "the_password_for_the_above_address"; /* The password associated with the above username */
    /* Add your SQL database credentials in the below 4 lines*/
    $databaseHost = "localhost"; /* put the address/hostname of your database here */
    $dbUsername = "root"; /* put an admin username here, or at least one with write (s) */
    $dbPassword = ""; /* put the corresponding admin password here */
    $databaseName = "testdb"; /* The name of the database the script should look at */
    /* ######################################### Required Configuration 'Settings' End ######################################### */



    /* ######################################## Optional Configuration 'Settings' Start ######################################## */
    /* These are here just encase you change your table names */
    $cT = "crew_tab"; /* The name of the first table the script should look at */
    $eT = "entry_tab"; /* The name of the second table the script should look at */
    /* ######################################### Optional Configuration 'Settings' End ######################################### */

    $fieldSeparator = ",";
    $lineSeparator = "\n";
    $csvFile = 'csv.csv';


    if(!file_exists($csvFile)) {
    die("No CSV, Skipping to next email");
    }
    try {
        /* try to make a connection to the SQL server*/
        $twoDArray = array();
        if (($handle = fopen("csv.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 0, $fieldSeparator)) !== FALSE) {
                $twoDArray[] = $data;
            }
            fclose($handle);
        }
        echo $twoDArray[0][1];
        $database = new PDO("mysql:host=$databaseHost;dbname=$databaseName", $dbUsername, $dbPassword,
            array(
                PDO::MYSQL_ATTR_LOCAL_INFILE => true,
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            )

        );
    } catch (PDOException $e) {
        die("Database connection failed: ".$e->getMessage());
    }
    foreach($twoDArray as $oneDArray){
        foreach($oneDArray as $row){
        $affectedRows = $database->exec("
        LOAD DATA LOCAL INFILE ".$database->quote($row)." INTO TABLE `$eT`
          FIELDS TERMINATED BY ".$database->quote($fieldSeparator)."
          LINES TERMINATED BY ".$database->quote($lineSeparator));
        }
    }
    echo "\nLoaded a total of $affectedRows records from the csv file into the database.\n";


    /* try to make a connection to the email server*/
    $inbox = imap_open($mailbox,$emailAddress,$emailPassword) or die('Failed to connect to Gmail: ' . imap_last_error());
    set_time_limit(3000);
    $emailAddresss = imap_search($database, 'FROM "abc@gmail.com"');
    /* if any emails are found, the script will iterate through each of them */
    if($emailAddresss) {
        $count = 1;
        /* sorts by newest first */
        rsort($emailAddresss);
        /* for every value in emailAddress... */
        foreach($emailAddresss as $emailAddressNumber){
            /* get information specific to this email */
            $overview = imap_fetch_overview($inbox,$emailAddressNumber,0);
            $message = imap_fetchbody($inbox,$emailAddressNumber,2);
            /* get mail structure */
            $structure = imap_fetchstructure($inbox, $emailAddressNumber);
            $attachments = array();
            /* if any attachments found... */
            if(isset($structure->parts) && count($structure->parts)){
                for($i = 0; $i < count($structure->parts); $i++){
                    $attachments[$i] = array(
                        'is_attachment' => false,
                        'filename' => '',
                        'name' => '',
                        'attachment' => '',
                    );
                    if($structure->parts[$i]->ifdparameters){
                        foreach($structure->parts[$i]->dparameters as $object){
                            if(strtolower($object->attribute) == 'filename')
                            {
                                $attachments[$i]['is_attachment'] = true;
                                $attachments[$i]['filename'] = $object->value;
                            }
                        }
                    }
                    if($structure->parts[$i]->ifparameters){
                        foreach($structure->parts[$i]->parameters as $object){
                            if(strtolower($object->attribute) == 'name'){
                                $attachments[$i]['is_attachment'] = true;
                                $attachments[$i]['name'] = $object->value;
                            }
                        }
                    }
                    if($attachments[$i]['is_attachment']){
                        $attachments[$i]['attachment'] = imap_fetchbody($inbox, $emailAddressNumber, $i+1);
                        /* Extracts the email contents into usable text, 3 = BASE64 encoding*/
                        if($structure->parts[$i]->encoding == 3){
                            $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                        }
                        /* 4 = QUOTED-PRINTABLE encoding */
                        elseif($structure->parts[$i]->encoding == 4){
                            $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                        }
                    }
                }
            }
            /* iterate through each attachment and save it */
            foreach($attachments as $attachment){
                if(($attachment['is_attachment'] == 1) )
                {
                    $filename = $attachment['name'];
                    if(empty($filename)) $filename = $attachment['filename'];

                    if(empty($filename)) $filename = time() . ".dat";
                    $folder = "attachment";
                    if(!is_dir($folder)){
                         mkdir($folder);
                    }
                    $fp = fopen("./". $folder ."/". $emailAddressNumber . "-" . $filename, "w+");
                    fwrite($fp, $attachment['attachment']);
                    fclose($fp);
                }
            }
        }
    }
    /* close the connection to the email_address server */
    imap_close($inbox);

    echo "####################Script ran with no errors####################";
?>

你能试试下面的方法吗

    $query = "LOAD DATA LOCAL INFILE ".$database->quote($row)." INTO TABLE `$eT`
              FIELDS TERMINATED BY ".$database->quote($fieldSeparator)."
              LINES TERMINATED BY ".$database->quote($lineSeparator));


$affectedRows = $database->exec($query);
但是要检查查询,请尝试
die($query)在执行之前,打印查询并查看有什么问题