簡體   English   中英

使用php將txt文件加載到mysql數據庫中

[英]Load txt file into mysql database using php

我正在嘗試從文件填充數據庫,但出現以下錯誤:無法加載。 用戶'username'@'localhost'的訪問被拒絕(使用密碼:是)。 我正在使用免費的虛擬主機服務。 我該如何解決?

<?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();
}
?>

用戶'user'@'localhost'的訪問被拒絕錯誤可能意味着:

  1. '用戶'@'本地主機'沒有FILE特權。 您應該授予此特權。 或者,您應該檢查所使用的配置是否正確(用戶存在並且在相應的數據庫上具有所有必需的特權)

  2. 您要加載的文件在運行mysql服務器的計算機上不存在

如果要從計算機加載文件,則應改用LOAD DATA LOCAL INFILE。

  1. 檢查您嘗試加載的文件是否可讀,請嘗試使用chmod 755讀取該文件以及“ myFile.txt”的所有父目錄)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM