麻烦读文本文件php

时间:2017-02-21 02:42:57

标签: php mysqli

我似乎无法将文本文件正确读入mysql。我有2列artid和artname,这两列似乎都在我的mysql的一列之下。我的代码有问题吗?

txt文件

1   Acconci
2   Ames
3   Aserty
4   Baron
5   Battenberg
6   Bindner

php code

<?php

$servername = "localhost";  //mysql
$username = "root";
$password = "";
$dbname = "project1";            //db525
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully <br />";


$handle = @fopen("artists.txt", "r"); //read line one by one
$values='';

while (!feof($handle)) // Loop til end of file.
{
    $buffer = fgets($handle, 4096); // Read a line.
   // list($a, $b)=explode(" ",$buffer);//Separate string by 
    list($a, $b) = array_pad(explode(' ', $buffer, 2), 2, null);


    echo $a." ".$b."<br>";

    $sql = "INSERT into arts(ArtistID,ArtistName) VALUES('".$a."','".$b."')";
                mysqli_query($conn, $sql) or die (mysqli_error($conn));


    // use mysql insert query here
}
  ?>

0 个答案:

没有答案