读取目录然后插入到sql
出于某种原因,我对数组很愚蠢...我无法完全弄清楚它们...我在这里想做的就是读取一个文件夹并获取该文件夹内的所有文件夹名称并将它们插入到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为通配符丢失了。
试试这个:
I think the wild-card is missing.
Try this: