How to extract data from multiple text files and insert into mysql?

krj7709

New Member
Trying to inset data from multiple text files into a mysql database.

All text files are in same directory.
All text files contain 9 lines of data.
Each line is a separate field in the database.
Dir layout
  • phpscript.php
  • data
    • txt.files.txt


This is what I have so far...

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test script.</title>
</head>

<body>
<?php
    $host = "localhost";
    $username = "testing";
    $password = "*******";
    $database = "zadmin_testing";
  
    // Creating my connection
{
    $conn = mysql_connect("$host","$username","$password");
    //Checking connection
    if (!$conn)
        {
            die('Could not connect: ' . mysql_error());
        }
}

    $textFilesArray = scandir("data");
        foreach( $textFilesArray AS $textFile )
            {
                $handle = fopen(data/$textFile,"r");
                # Get the lines of the file
                $dateCreated = fgets($handle);
                $videoTitle = fgets($handle);
                $videoType = fgets($handle);
                $videoImage = fgets($handle);
                $videoGenre = fgets($handle);
                $videoViewerRating = fgets($handle);
                $videoPath = fgets($handle);
                $videoMovieRating = fgets($handle);
                $videoDesc = fgets($handle);
            }


    //My query
    $query = "INSERT INTO `zadmin_testing`.`videos` (`date`, `title`, `type`, `cover`, `genre`, `vrating`, `path`, `mrating`, `description`) VALUES ('$dateCreated',   '$videoTitle', '$videoType', '$videoImage', '$videoGenre', '$videoViewerRating', '$videoPath', '$videoMovieRating', '$videoDesc')";

{
    //Run my query
    mysql_query($query);
}

echo "<h2>testing 12</h2>";

  mysql_close($conn);

?>
</body>
</html>

So far all that it's doing is inserting a blank row into the database.

What am I missing or doing wrong?
 

chrishirst

Well-Known Member
Staff member
have you output your variables and/or your query to the browser instead of sending it to the database server?
 
Top