簡體   English   中英

多個txt文件到mysql條目腳本

[英]multiple txt files to mysql entries script

好的,這是我的情況。

我使用一個腳本來掃描視頻目錄,該腳本檢索視頻上的imdb信息,然后將數據存儲在文本文件中。 每部電影1張。 我想從txt文件中獲取數據並插入數據庫中。

該腳本將需要檢查以查看有什么新內容,哪些沒有,僅插入新信息。

這就是所有txt文件的布局方式。

09/04/2015 02:11:54
Cast Away
movie
tt0162222
Adventure,Drama
7.7
videos/Cast.Away.2000.mp4
pg_13
A FedEx executive must transform himself physically and emotionally to survive a crash landing on a deserted island.

每行說明

Line 1 - Date txt/time file created
Line 2 - Title of Movie
Line 3 - Type of video (Movie or TV show)
Line 4 - image file associated with movie
Line 5 - Genre
Line 6 - Viewer Rating (0.0 to 10)
Line 7 - video directory
Line 8 - Movie Rating
Line 9 - Description

因此,基本上我需要弄清楚如何將txt數據放入mysql數據庫中,以便可以在家庭媒體服務器上使用它。 基本上,我正在為我的大型DVD收藏集制作一個類似於netflix的網站。

順便說一句,目錄布局是

/data  - (txt files)
/pics  - (cover photos)
/videos - (video files)
script.php

這是更新的代碼(9/5/2015 4:22)仍然是相同的結果。 空白行。

<!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";
    $textFilesArray = scandir("data");
        foreach( $textFilesArray AS $textFile )
            {
                $handle = fopen($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);
            }

    // Creating my connection
{
    $conn = mysql_connect("$host","$username","$password");
    //Checking connection
    if (!$conn)
        {
            die('Could not connect: ' . mysql_error());
        }
}
    //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>

假設要導入數據庫的所有文本文件都在一個文件夾中,這就是我要做的:

首先,使用scandir()獲取目錄中所有文本文件的列表。

$textFilesArray = scandir("directoryContainingTheFiles"); 

然后,您可以遍歷文件

foreach( $textFilesArray AS $textFile ){

從文件中讀取所有數據。

# Open the file for reading (r)....
$handle = fopen($textFile, "r");

# Get the first line of the file
$dateCreated = fgets($handle);

# get the next line from the file
$movieTitle = fgets($handle);

現在,您有幾種選擇來避免重復記錄。

一種選擇是在文件的開頭/結尾保存一個標志,當該標志存在時,跳過插入影片的嘗試。

另一種選擇是在您的mySQL字段之一上創建唯一鍵。 (也許是電影標題)。 然后,當您插入時,將不會創建重復項。 (重復輸入嘗試將導致錯誤,這意味着什么都不會發生。如果要抑制這些mySQL錯誤,可以始終使用“ INSERT IGNORE ....”代替“ INSERT ....”。)

因此總而言之:

$textFilesArray = scandir("directoryContainingTheFiles"); 

foreach( $textFilesArray AS $textFile ){

    # Open the file for reading (r)....
    $handle = fopen($textFile, "r");

    # Get the first line of the file
    $dateCreated = fgets($handle);

    # get the next line from the file
    $movieTitle = fgets($handle);

    # get the rest of the file into variables here

    # preform checks on variables before attempting the insert
    # (make sure strings are escaped, and number values contain all numbers, etc.)

    # PREFORM mySQL INSERT QUERY HERE!
    # something like: "INSERT INTO movies (date, title, etc) VALUES ($dateCreated, $movieTitle, etc)"
}

暫無
暫無

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

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