Hi all. Just getting back into PHP and creating my first app. What better way to start than to automate something to help you in your day job.
The script is nothing complicated, I write a .csv file from an array and then load it into MySQL using “LOAD DATA LOCAL INFILE”.
I am using this load method as the file consists of circa 40k lines of data.
The problem I am having is that I’m getting the error:-
Fatal error : Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. in C:\xampp\htdocs\
I have literally stripped this right back to the folllowing script, so I know there are no other queries running.
My PDO Connection:
$pdo = new PDO($dsn, $db_user, $db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
$pdo->setAttribute(PDO::MYSQL_ATTR_LOCAL_INFILE, true);
The Script:
$fp = fopen('thefile.csv', 'w');
// code here that writes an array into the file with fputcsv($val1, $val2, $val3, intval($val4);
$qry = "LOAD DATA LOCAL INFILE 'thefile.csv' INTO TABLE thetable FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n' (col1, col2, col3, col4)";
if($pdo->query($qry))
{
fclose($fp); unlink("thefile.csv");
exit('Success');
} else {
exit("Failed");
}
Scratching my head over this one and my best friend Google is not turning up trumps for me, so welcome any input you can give, even if it is recommending an alternative [fast load] method for putting large data sets into MySQL.
I did read that there is a potential MySQL bug, so I went ahead and uninstalled\reinstalled XAMPP, but still nothing.
I am working in the XAMPP environment by the way (PHP 7.3.27 & 10.4.17-MariaDB)
Thanks in advance
/Danny