扫描文件目录并将文本文件数据插入mysql

时间:2015-09-06 03:53:50

标签: php mysql text insert

在Sephedo和Varun的帮助下,我终于得到了我想要的东西。这是代码....

现在发生的事情是因为txt文件数据中的错误导致mysql错误语法错误。我将如何以及在何处进行sql查询,忽略可能会抛出mysql的其他字符

<?php

//Open connection
    $servername = "localhost";
    $username = "testing";
    $password = "*******";
    $dbname = "zadmin_testing";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
/**
 * @author - Sephedo
 * @for - Randall @ Stackoverflow
 * @question - http://stackoverflow.com/questions/18704981/read-each-line- of-text-of-each-file-in-a-directory-and-place-into-array/18705231#18705231
 */

$directory = 'data/'; // The directory to the lesson text files

$linesToReturn = 9; // Set to nine for the number of lines in each text file  ( for expansion? )

// Find all files in the directory which are .txt files
foreach( glob( $directory . "*.txt" ) as $filename )
{
    // Open the file
    if( $handle = @fopen( $filename, "r") )
    {
        $x = 0; // Start the line counter

        // Cycle each line until end or reach the lines to return limit
        while(! feof( $handle ) or $x < $linesToReturn )
        {
            $line = fgets($handle); // Read the line

            $lessons[$filename][] = $line;

            $x++; // Increase the counter
        }

        // This lines makes sure that are exactly x# of lines in each lesson
        if( count( $lessons[$filename] ) != $linesToReturn ) unset(   $lessons[$filename] );
    }
}

// creates a blank list if no files or valid files were found.
if(! isset( $lessons ) ) $lessons = array();
foreach( $lessons AS $file => $details )

//Creates your data into a query
        {

            $sql = "INSERT INTO videos (date, title, type, cover, genre, vrating, path, mrating, description) VALUES ('$details[0]', '$details[1]', '$details[2]', '$details[3]', '$details[4]', '$details[5]', '$details[6]', '$details[7]', '$details[8]')";
                if (mysqli_query($conn, $sql)) {
                echo "New record created successfully <br>";
                    } else {
                    echo "Error: " . $sql . "<br>" . mysqli_error($conn);

        }
        }
//Close mysql connection
 mysqli_close($conn);

?>

我开始的代码     

/**
 * @author - Sephedo
 * @for - Randall @ Stackoverflow
 * @question - http://stackoverflow.com/questions/18704981/read-each-line- of-text-of-each-file-in-a-directory-and-place-into-array/18705231#18705231
 */

$directory = 'lessons/'; // The directory to the lesson text files

$linesToReturn = 4; // Set to four for the number of lines in each text file ( for expansion? )

// Find all files in the directory which are .txt files
foreach( glob( $directory . "*.txt" ) as $filename )
{
    // Open the file
    if( $handle = @fopen( $filename, "r") )
{
    $x = 0; // Start the line counter

    // Cycle each line until end or reach the lines to return limit
    while(! feof( $handle ) or $x < $linesToReturn )
    {
        $line = fgets($handle); // Read the line

        $lessons[$filename][] = $line;

        $x++; // Increase the counter
    }

    // This lines makes sure that are exactly 4 lines in each lesson
    if( count( $lessons[$filename] ) != $linesToReturn ) unset(       $lessons[$filename] );
    }
}

// creates a blank list if no files or valid files were found.
if(! isset( $lessons ) ) $lessons = array();

// The rest of the page just builds a simple table to display each lesson.-
echo '<h1>Lesson Plans</h1>';
echo '<table>';
echo '<th>Lesson Number</th><th>Lesson Title</th><th>Description</th><th>Due    Date</th>';

foreach( $lessons as $file => $details )
{
    echo '<tr><td>' . $details[0] . '</td><td>' . $details[1] . '</td><td>' .    $details[2] . '</td><td>' . $details[3] . '</td></tr>';
}

echo '</table>';

?>

0 个答案:

没有答案