当此文件夹中有数千条记录时,此计数仅返回 1。
// TV Shows
$dir = 'G:/TV';
if ($handle = opendir($dir)) {
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
$check = mysql_query("SELECT * FROM tv_shows WHERE title = '$file'");
$checkcount = mysql_num_rows($check);
if ($checkcount == 0) {
mysql_query("INSERT INTO tv_shows (id, title) VALUES ('', '$file')");
}
}
echo count($dir)." Records Traversed!<br/>";
closedir($handle);
}
表结构:id、标题
文件夹结构:主文件夹中的子文件夹
G:\TV
G:\TV\24
G:\TV\Family Guy
最佳答案
您不能对文件夹使用count()
。试试这个吧
// TV Shows
$dir = 'G:/TV';
$count = 0;
if ($handle = opendir($dir)) {
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
$count++;
$check = mysql_query("SELECT * FROM tv_shows WHERE title = '$file'");
$checkcount = mysql_num_rows($check);
if ($checkcount == 0) {
mysql_query("INSERT INTO tv_shows (id, title) VALUES ('', '$file')");
}
}
echo $count." Records Traversed!<br/>";
closedir($handle);
}
how about returning the number of new records inserted?
看看这个,刚刚移动了$count++
// TV Shows
$dir = 'G:/TV';
$count = 0;
if ($handle = opendir($dir)) {
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
$check = mysql_query("SELECT * FROM tv_shows WHERE title = '$file'");
$checkcount = mysql_num_rows($check);
if ($checkcount == 0) {
$count++;
mysql_query("INSERT INTO tv_shows (id, title) VALUES ('', '$file')");
}
}
echo $count." Records Traversed!<br/>";
closedir($handle);
}
关于php - count() 当有数千条记录时返回 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8306613/