php - Creating a database table using a CSV file by making PDO calls -
i have been trying create database using first row of csv file, keep getting error pdo syntax. apparently going wrong varchar(250) in $columns
variable:
fatal error: uncaught exception 'pdoexception' message 'sqlstate[42000]: syntax error or access violation: 1064 have error in sql syntax; check manual corresponds mysql server version right syntax use near 'varchar(250))' @ line 1
the code below:
<? /********************************************************************************/ // parameters: filename.csv table_name $argv = 'seo_domains.csv seo_domains'; if($argv[1]) { $file = $argv[1]; } else { echo "please provide file name\n"; exit; } if($argv[2]) { $table = $argv[2]; } else { $table = pathinfo($file); $table = 'seo_domains'; } /********************************************************************************/ // first row create column headings $fp = fopen('seo_domains.csv', 'r'); $frow = fgetcsv($fp); $ccount = 0; foreach($frow $column) { $ccount++; if($columns) $columns .= ', '; $columns .= "$column varchar(250)"; } $qry = $dbcon->prepare("create table if not exists $table ($columns);"); $qry->execute(); /********************************************************************************/ // import data newly created table. $file = $file; $qry = $dbcon->prepare("load data infile '$file' table $table fields terminated ',' ignore 1 lines"); $qry->execute(); ?>
i changed $fp
fopen static value instead of using $file
because apparently guy coded code mentioned above, did not mention how format $argv
variable. yes, tried formatting according "parameters" comment on first line still, no avail. statically changed $table
variable 'seo_domains' since $argv
variable not being split properly. instead of re-coding above code, wondering if has thoughts why database resulting in described error. appreciated. trying create table based on first row of csv file provided. upon creation continue insert row values below row 1 in csv, per usual.
try:
create table if not exists seo_domains (domain varchar(250), server varchar(250), ip varchar(250), username varchar(250), password varchar(250), nameserver varchar(250), namecheap varchar(250), ga varchar(250), wmt varchar(250), sb varchar(250), gaw varchar(250), notes varchar(250), blankfield varchar(250), id int key not null auto_increment varchar(250));
Comments
Post a Comment