IGNORE 1 LINES in MySQL PDO PHP Command Line Not Working -
i using php script via command line load csv file mysql via pdo statement. import executes successfully... except first line not being ignored ignore 1 lines
the csv file: http://federalgovernmentzipcodes.us/free-zipcode-database.csv
(error checking removed brevity, full sql left in completeness)
<?php //import_zips.php $db_host = 'localhost'; $db_user = 'dbuser'; $db_pass = 'dbpass'; $db_name = 'dbname'; $input_file = "free-zipcode-database.csv"; $fieldseperator = ","; $lineseperator = "\n"; $fieldsenclosed = '"'; $test_table = "test_zipcodes"; if(!file_exists($input_file)) { die("file not found."); } $pdo = new pdo("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass, array( pdo::mysql_attr_local_infile => true, pdo::attr_errmode => pdo::errmode_exception ) ); $pdo->exec("drop table if exists ".$test_table); $pdo->exec("create table `".$test_table."` (`id` int(11) unsigned not null auto_increment, `zipcode` int(11) default null, `type` varchar(10) default null, `city` varchar(30) default null, `state` varchar(2) default null, `location_type` varchar(20) default null, `lat` double default null, `lon` double default null, `xaxis` double default null, `yaxis` double default null, `zaxis` double default null, `worldregion` char(2) default null, `country` char(2) default '', `location_text` varchar(40) default null, `location` varchar(60) default null, `decommisioned` char(5) default null, `tax_returns_filed` bigint(20) default null, `estimated_population` bigint(20) default null, `total_wages` bigint(20) default null, primary key (`id`)) engine=innodb auto_increment=1 default charset=utf8;"); try { $affectedrows = $pdo->exec(" load data local infile ".$pdo->quote($input_file)." table `$test_table` fields terminated ".$pdo->quote($fieldseperator)." optionally enclosed ".$pdo->quote($fieldsenclosed)." lines terminated ".$pdo->quote($lineseperator))." ignore 1 lines"; echo "loaded total of $affectedrows records.\n"; } catch (pdoexception $e) { die($e->getmessage()."\n"); } the script executes (with side effect of ignore 1 lines appended $affectedrows.)
$ php import_zips.php loaded total of 81831 ignore 1 lines records. the problem first row of csv not being ignored , ends in table. basically, ignore 1 lines being ignored.
i have workaround (manually deleting first line of csv) have better solution how work correctly.
php version
php 5.5.23 (cli) (built: mar 23 2015 01:47:37) mysql version
mysql ver 14.14 distrib 5.5.9, osx10.6 (i386) using readline 5.1 running on mac os x 10.10.3
correction:
$affectedrows = $pdo->exec(" load data local infile ".$pdo->quote($input_file)." table `$test_table` fields terminated ".$pdo->quote($fieldseperator)." optionally enclosed ".$pdo->quote($fieldsenclosed)." lines terminated ".$pdo->quote($lineseperator)." ignore 1 lines");
Comments
Post a Comment