How can access denied errors for LOAD DATA INFILE be resolved when importing data into a MySQL database using PHP scripts?
To resolve access denied errors for LOAD DATA INFILE when importing data into a MySQL database using PHP scripts, you can adjust the MySQL user's permissions to allow the FILE privilege. This privilege grants the user the ability to read and write files on the server, which is necessary for using LOAD DATA INFILE.
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Grant FILE privilege to user
$mysqli->query("GRANT FILE ON *.* TO 'username'@'localhost'");
// Import data using LOAD DATA INFILE
$mysqli->query("LOAD DATA INFILE '/path/to/file.csv' INTO TABLE tablename FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'");
// Close database connection
$mysqli->close();
Related Questions
- What are some best practices for structuring PHP code when dealing with nested queries and loops for displaying data?
- What are some best practices for handling leap years in PHP when performing date calculations or comparisons?
- How can returning an array of variables from a function be beneficial in PHP programming?