简体   繁体   中英

Load txt file into mysql database using php

I am trying fill in a database from a file but I am getting the following error : Could not load. Access denied for user 'username'@'localhost' (using password: YES). I am using a free webhosting service. How do I resolve this?

<?php
require 'connect.inc.php';
$string = file_get_contents("testdata.txt"); 
$myFile = "myFile.txt";
$fileContents = file_get_contents('testdata.txt');
fwrite($fh, $string);
fclose($fh);
mysql_query('TRUNCATE TABLE table1;'); //clear the existing table
$result = mysql_query("LOAD DATA INFILE 'myFile.txt'" .
                      " INTO TABLE table1 FIELDS TERMINATED BY '|'");
if (!$result) {
    die("Could not load. " . mysql_error());
}
echo 'OK8';
$query = "SELECT * FROM table1"; 
if ($query_run = mysql_query($query)){
    echo 'Query success.';
    if (mysql_num_rows($query_run)==NULL){
        echo 'No results found.';
    } else {
    while ($query_row = mysql_fetch_assoc($query_run)) {
        echo json_encode($query_row);
    }
    }
}else{
    echo mysql_error();
}
?>

The access denied error for user 'user'@'localhost' could mean that:

  1. the 'user'@'localhost' doesn't have the FILE privilege. you should grant this privilege. Or you should check that the configuration you are using is ok (user exists and has all the necessary privileges on the corresponding database)

  2. the file you are trying to load does not exist on the machine running mysql server

If you want to load the file from your machine you should use LOAD DATA LOCAL INFILE instead.

  1. Check that the file you are trying to load is readable try chmod 755 for the file and for all the parent directories of 'myFile.txt')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM