读取目录然后插入到sql

发布于 2024-12-06 07:29:34 字数 823 浏览 0 评论 0原文

出于某种原因,我对数组很愚蠢...我无法完全弄清楚它们...我在这里想做的就是读取一个文件夹并获取该文件夹内的所有文件夹名称并将它们插入到 mysql 数据库中。现在数据库部分不是我的问题。它处理这个数组。我在早期的项目中做了类似的事情,现在我不知道如何修改它以在这里工作。

<?php 
$main_folder = 'C:/Users/Oval Office/Music/'; 
$folders = glob($main_folder, GLOB_ONLYDIR); 

$artists_names = array(); 
foreach($folders as $folder){ 
    $artists_names[] = preg_split('/(.+)\s(\d+)/', str_replace($main_folder, '', $folder), -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);  
} 

$values = array(); 
foreach($artists_names as $pair){ 
    $values[] = "('".$pair[0]."')"; 
} 

$query = 'INSERT INTO artists (title) VALUES '.implode(',', $values); 
$result = mysql_query($query); 
echo ($result) ? 'Inserted successfully' : 'Failed to insert the values';
?> 

标题只是空白...

谢谢佩卡引导我走向正确的方向,将其作为答案发布,以便我可以给您一些代表。

For some reason I am just dumb with arrays... I cannot figure them all the way out... All I want to do here is read a folder and get all the folder names inside of that folder and insert them into a mysql db. Now the db part isn't my problem. Its handling this array. I did something similar in an earlier project, and now I cannot figure out how to modify it to work here.

<?php 
$main_folder = 'C:/Users/Oval Office/Music/'; 
$folders = glob($main_folder, GLOB_ONLYDIR); 

$artists_names = array(); 
foreach($folders as $folder){ 
    $artists_names[] = preg_split('/(.+)\s(\d+)/', str_replace($main_folder, '', $folder), -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);  
} 

$values = array(); 
foreach($artists_names as $pair){ 
    $values[] = "('".$pair[0]."')"; 
} 

$query = 'INSERT INTO artists (title) VALUES '.implode(',', $values); 
$result = mysql_query($query); 
echo ($result) ? 'Inserted successfully' : 'Failed to insert the values';
?> 

The Title Is Just Blank...

Thank you pekka for stearing me in the right direction, post it as a answer so I can give ya some rep.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦屿孤独相伴 2024-12-13 07:29:34

我认为通配符丢失了。

试试这个:

$main_folder = 'C:/Users/Oval Office/Music/*'; 

I think the wild-card is missing.

Try this:

$main_folder = 'C:/Users/Oval Office/Music/*'; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文