Question

I have been trying to create a database by using the first row of a CSV file, however I keep getting an error about my PDO syntax. Apparently something is going wrong with varchar(250) in the $columns variable:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(250))' at line 1

The code is below:

<?
/********************************************************************************/
// Parameters: filename.csv table_name

$argv = 'seo_domains.csv seo_domains';

if($argv[1]) { $file = $argv[1]; }
else {
    echo "Please provide a file name\n"; exit; 
}
if($argv[2]) { $table = $argv[2]; }
else {
    $table = pathinfo($file);
    $table = 'seo_domains';
}

/********************************************************************************/
// Get the first row to create the column headings

$fp = fopen('seo_domains.csv', 'r');
$frow = fgetcsv($fp);

$ccount = 0;
foreach($frow as $column) {
    $ccount++;
    if($columns) $columns .= ', ';
    $columns .= "$column varchar(250)";
}

$qry = $dbcon->prepare("CREATE TABLE if not exists $table ($columns);");
$qry->execute();

/********************************************************************************/
// Import the data into the newly created table.

$file = $file;
$qry = $dbcon->prepare("load data infile '$file' into table $table fields terminated by ',' ignore 1 lines");
$qry->execute();
?>

I changed the $fp fopen to a static value instead of using $file because apparently the guy who coded the code mentioned above, did not mention how to format the $argv variable. Yes, I already tried formatting according to his "Parameters" comment on the first line but still, no avail. I also statically changed the $table variable to 'seo_domains' since the $argv variable is not being split properly. Instead of re-coding the above code, I am wondering if anyone has any thoughts as to why my database would be resulting in the described error. Any help is appreciated. Just trying to create a table based on first row of the CSV file provided. Upon creation I would like to continue to insert all the row values below row 1 in the CSV, per usual.

Was it helpful?

Solution

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));

OTHER TIPS

key is a reserved word in SQL. See image below

enter image description here

As you may find that some of the column names in CSV could be reserved words you will need to escape them. In MySQL backticks are used. ie `key`

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top