简体   繁体   English

使用php将txt文件加载到mysql数据库中

[英]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). 用户'username'@'localhost'的访问被拒绝(使用密码:是)。 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: 用户'user'@'localhost'的访问被拒绝错误可能意味着:

  1. the 'user'@'localhost' doesn't have the FILE privilege. '用户'@'本地主机'没有FILE特权。 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 您要加载的文件在运行mysql服务器的计算机上不存在

If you want to load the file from your machine you should use LOAD DATA LOCAL INFILE instead. 如果要从计算机加载文件,则应改用LOAD DATA LOCAL INFILE。

  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') 检查您尝试加载的文件是否可读,请尝试使用chmod 755读取该文件以及“ myFile.txt”的所有父目录)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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