数据作为数组而不是值存储在数据库中

时间:2018-03-19 12:12:43

标签: php arrays wamp mp3

以下代码读取包含.mp3文件的目录并获取其标签信息,如Title,Genre等,并将这些信息存储在WAMP数据库中,但是因为它读取目录并将文件存储在数组中,所以数据库显示为每个列字段的“数组”,但我希望显示每个文件的值。有谁知道如何纠正我的代码才能实现这一点,谢谢!

我的代码:

$sDir = 'mp3/';
$aFiles = array();
$rDir = opendir($sDir);
if ($rDir) {
    while ($sFile = readdir($rDir)) {
        if ($sFile == '.' or $sFile == '..' or !is_file($sDir . $sFile))
            continue;

        $aPathInfo = pathinfo($sFile);
        $sExt = strtolower($aPathInfo['extension']);
        if ($sExt == 'mp3') {
            $aFiles[] = $sDir . $sFile;
        }
    }
    closedir($rDir);
}

// new object of our ID3TagsReader class
$oReader = new ID3TagsReader();

// passing through located files ..
$sList = $sList2 = '';
foreach ($aFiles as $sSingleFile) {
    $Title = $oReader->getTagsInfo($sSingleFile); // obtaining ID3 tags info
    $Author = $oReader->getTagsInfo($sSingleFile);
    $AlbumAuthor = $oReader->getTagsInfo($sSingleFile);
    $Year = $oReader->getTagsInfo($sSingleFile);
    $Genre = $oReader->getTagsInfo($sSingleFile);


    $sList .= '<tr><td>'.$Title['Title'].'</td><td>'.$Author['Author'].'</td><td>'.$AlbumAuthor['AlbumAuthor'].'</td>
                <td>'.$Year['Year'].'</td><td>'.$Genre['Genre'].'</td></tr>';



$dbhost = "localhost";
$dbname = "project";
$dbusername = "root";
$dbpassword = "";

$link = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbusername, $dbpassword);

$statement = $link->prepare("INSERT INTO mp3(Title, Author, AlbumAuthor, Year, Genre)
VALUES(:Title, :Author, :AlbumAuthor, :Year, :Genre)");
$statement->execute(array(
"Title" => "$Title",
"Author" => "$Author",
"AlbumAuthor" => "$AlbumAuthor",
"Year" => "$Year",
"Genre" => "$Genre"
));

1 个答案:

答案 0 :(得分:0)

您可能会发现调用->getTagsInfo()会返回所有标记详细信息的数组。您可以通过执行

来验证这一点
print_r($Title);

阅读https://www.script-tutorials.com/id3-tags-reader-with-php/上的示例代码并假设一个类似的过程,他们有代码 -

$aTags = $oReader->getTagsInfo($sSingleFile); // obtaining ID3 tags info
$sList .= '<tr><td>'.$aTags['Title'].'</td><td>'.$aTags['Album'].'</td><td>'.$aTags['Author'].'</td>

因此,重要的部分是$aTags是所有标记的列表,第二行使用$aTags['Title']来获取标题。

所以你可能需要用......代替你的代码。

$dbhost = "localhost";
$dbname = "project";
$dbusername = "root";
$dbpassword = "";

$link = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbusername, $dbpassword);

$statement = $link->prepare("INSERT INTO mp3(Title, Author, AlbumAuthor, Year, Genre)
VALUES(:Title, :Author, :AlbumAuthor, :Year, :Genre)");
foreach ($aFiles as $sSingleFile) {
    $aTags = $oReader->getTagsInfo($sSingleFile); 
    $Title = $aTags['Title'];
    $Author = $aTags['Author'];
    $AlbumAuthor = $aTags['AlbumAuthor'];
    $Year = $aTags['Year'];
    $Genre = $aTags['Genre'];
    $sList .= '<tr><td>'.$Title.'</td><td>'.$Author.'</td><td>'.$AlbumAuthor.'</td> <td>'.$Year.'</td><td>'.$Genre.'</td></tr>';

    if ( !$statement->execute(array(
            "Title" => $Title,
            "Author" => $Author,
            "AlbumAuthor" => $AlbumAuthor,
            "Year" => $Year,
            "Genre" => $Genre )))      {
        echo "Error:".print_r($link->errorInfo(),true);
    }
}