将动态csv导入MySQL

时间:2013-06-04 20:20:47

标签: php mysql csv

我正在寻找一种导入csv文件的方法,该文件的列每次都可能有也可能不是相同的顺序。也有些时候可能会有额外的列..

这是我到目前为止所拥有的...... 它遍历每一行并将其插入mysqldb。

我的问题是columsn和column命令有时会改变,而不是列的名称。他们只是添加和删除列。

我试图避免让我的客户端在导入之前必须彻底修改csv。

我希望脚本能够在csv中查看列名,并自动将其与表中的列匹配。

而不是依赖于$ data [1] ...指定一个特定的列。

// Process Scrubbed acounts import
if ($_FILES['scrubImport']['tmp_name']) {

    // Get the Rep Account ID
    ${'Rep Account'} = $_POST['importToRep'];

    // Get our total # of contacts
    $contacts = mysql_result(mysql_query('SELECT count(0) FROM `accounts` WHERE `owner` = '.${'Owner'}), 0);

    // Get our import file extension
    ${'Extension'} = strtolower(array_pop(explode('.', $_FILES['scrubImport']['name'])));

    // Store our contacts array
    ${'Accounts'} = array();
    // If our file is a CSV
    if (${'Extension'} == 'csv') {

        // Get our row data
        ini_set('auto_detect_line_endings',true);
        if (($handle = fopen($_FILES['scrubImport']['tmp_name'], "r")) !== FALSE) {
            while (($data = fgetcsv($handle)) !== FALSE) {

                // Sanitize all our data
                foreach ($data as $i => $v) { $data[$i] = inputCleanText($v); }

                // Add the data to our contacts array
                if($data[0] != 'Keyword'){
                    ${'Accounts'}[] = array('Keyword' => $data[0], 'Company Name' => $data[1], 'Owner First Name' => $data[2], 'Owner Last Name' => $data[3], 'Email' => $data[4], 'Phone' => str_replace('-','',str_replace(' ','',str_replace('(','',str_replace(')','',$data[5])))), 'Ranking On The 1st Page' => $data[6], 'Citations' => $data[7], '1st Category' => $data[8], 'Categories' => $data[9], 'Google+ Score' => $data[10], 'Google Reviews #' => $data[11], 'Google Reviews Found' => $data[12], '3rd Party Site Details' => $data[13], '3rd Party Site Summary' => $data[14], 'Google Bad Reviews #' => $data[15], 'Google Bad Reviews' => $data[16], 'IP Bad Reviews #' => $data[17], 'IP Bad Reviews' => $data[18], 'Images #' => $data[19], 'Videos #' => $data[20], 'Google+ Offers #' => $data[21], 'Google+ Offers Found' => $data[22], 'Groupon Offer' => $data[23], 'Groupon Offer Title' => $data[24], 'Groupon Purchase' => $data[25], 'Groupon Offer Expiration' => $data[26], 'Living Social Offer' => $data[27], 'Living Social Offer Title' => $data[28], 'Living Social Purchases' => $data[29], 'Living Social Expiration' => $data[30], 'Website URL' => $data[31], 'Keywords In Homepage' => $data[32], 'Mobile Optimized Site' => $data[33], 'Website Backlinks' => $data[34], 'Website Page Rank' => $data[35], 'Google+ URL' => $data[36], 'Owner Verified' => $data[37], 'Facebook Page URL' => $data[38], 'FB Likes #' => $data[39], 'FB Talking About This #' => $data[40], 'Twitter Page URL' => $data[41], 'Twitter Tweets #' => $data[42], 'Twitter Followers #' => $data[43], 'Twitter Page Found?' => $data[44], 'Address' => $data[45], 'City' => $data[46], 'State/Region' => $data[47], 'Postal Code' => $data[48], 'Country' => $data[49]);

                }
            }
        }
        fclose($handle);
    }

    // Start looping thru the rows
    $row = 0;
    $valid_count = 0;
    foreach (${'Accounts'} as ${'Account'}) {

        // Increase our valid count
        ++$valid_count;

        // Add the account
        mysql_query('INSERT INTO `accounts` (`owner`, `company_name`, `contact_fname`, `contact_lname`, `office_phone`, `email_primary`, `address_1`, `city`, `state`, `zip`, `relationship`, `acquired_via`, `website`) VALUES ("'.${'Rep Account'}.'", "'.${'Account'}['Company Name'].'", "'.${'Account'}['Owner First Name'].'", "'.${'Account'}['Owner Last Name'].'", "'.${'Account'}['Phone'].'", "'.${'Account'}['Email'].'", "'.${'Account'}['Address'].'", "'.${'Account'}['City'].'", "'.${'Account'}['State/Region'].'", "'.${'Account'}['Postal Code'].'", "Scrubbed Prospect", "Scrubbed Account", "'.${'Account'}['Website URL'].'")') or die(mysql_error());

        ${'New Account ID'} = mysql_insert_id();

        // Add the additional scrub info                        
        mysql_query('INSERT INTO `crm`.`accounts_scrubbed` (`account_id`,`keyword`,`ranking_first_page`,`citations`,`1st_category`,`categories`,`google_plus_score`,`google_reviews`,`num_google_reviews`, `3rd_party_site_details`,`3rd_party_site_summary`,`#_bad_google_reviews`,`google_bad_reviews`,`#_ip_bad_reviews`,`ip_bad_reviews`,`#_images`,`#_videos`,`google_plus_offers`,`google_plus_offers_found`,`groupon_offer`,`groupon_offer_title`,`groupon_purchase`,`groupon_offer_expiration`,`living_social_offer`,`living_social_offer_title`,`living_social_purchases`,`living_social_expiration`,`keywords_in_homepage`,`mobile_optimized_site`,`website_backlinks`,`website_page_rank`, `google_plus_url`,`owner_verified`,`facebook_page_url`,`#_facebook_likes`,`#_facebook_talking_about_this`,`twitter_page_url`,`#_twitter_tweets`,`#_twitter_followers`,`twitter_page_found`,`country`) VALUES ("'.${'New Account ID'}.'", "'.${'Account'}['Keyword'].'", "'.${'Account'}['Ranking On The 1st Page'].'", "'.${'Account'}['Citations'].'", "'.${'Account'}['1st Category'].'", "'.${'Account'}['Categories'].'", "'.${'Account'}['Google+ Score'].'", "'.${'Account'}['Google Reviews Found'].'", "'.${'Account'}['Google Reviews #'].'", "'.${'Account'}['3rd Party Site Details'].'", "'.${'Account'}['3rd Party Site Summary'].'", "'.${'Account'}['Google Bad Reviews #'].'", "'.${'Account'}['Google Bad Reviews'].'", "'.${'Account'}['IP Bad Reviews #'].'", "'.${'Account'}['IP Bad Reviews'].'", "'.${'Account'}['Images #'].'", "'.${'Account'}['Videos #'].'", "'.${'Account'}['Google+ Offers #'].'", "'.${'Account'}['Google+ Offers Found'].'", "'.${'Account'}['Groupon Offer'].'",  "'.${'Account'}['Groupon Offer Title'].'", "'.${'Account'}['Groupon Purchase'].'", "'.${'Account'}['Groupon Offer Expiration'].'", "'.${'Account'}['Living Social Offer'].'", "'.${'Account'}['Living Social Offer Title'].'", "'.${'Account'}['Living Social Purchases'].'", "'.${'Account'}['Living Social Expiration'].'", "'.${'Account'}['Keywords In Homepage'].'", "'.${'Account'}['Mobile Optimized Site'].'", "'.${'Account'}['Website Backlinks'].'", "'.${'Account'}['Website Page Rank'].'", "'.${'Account'}['Google+ URL'].'", "'.${'Account'}['Owner Verified'].'", "'.${'Account'}['Facebook Page URL'].'", "'.${'Account'}['FB Likes #'].'", "'.${'Account'}['FB Talking About This #'].'", "'.${'Account'}['Twitter Page URL'].'", "'.${'Account'}['Twitter Tweets #'].'",  "'.${'Account'}['Twitter Followers #'].'", "'.${'Account'}['Twitter Page Found?'].'", "'.${'Account'}['Country'].'")');

        // Add the generated report information
        mysql_query('INSERT INTO `crm`.`report` (`account_id`, `generated`, `sent`, `report_id`) VALUES ("'.${'New Account ID'}.'", "0", "0", "'.md5(microtime()).'")');

        // Add the last touched Record
        mysql_query('INSERT INTO `last_touched` (`account_id`, `user_id`, `reason`) VALUES ("'.${'New Account ID'}.'", "'.$_SESSION['userID'].'", "Imported Scrub")');

        ++$row;
    }

    // Post a message
    ${'Message'} = '<div class="success">Import successful. '.number_format($valid_count).' account(s) were imported sucessfully!</div>';

}

我不确定我是否完全清楚我想要做什么。

1 个答案:

答案 0 :(得分:2)

如果csv文件有标题记录,那么你可以这样做

  $fp = fopen($file, "r");
  $header = fgetcsv($fp);
  while ($line = fgetcsv($fp)){
       $record = array_combine($header, $line);
       // now record contains an array of fields keyed by the associated key in the header record
  }

从这里你可以引用while循环中的字段

  $record["fieldname"] 

这可以为您提供构建查询所需的内容。