php - count() 当有数千条记录时返回 1

标签 php count directory

当此文件夹中有数千条记录时,此计数仅返回 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/

相关文章:

perl - 计算包含数十万个文件的目录中文件数量的最快方法

logging - Equinox 独立日志文件夹

c++ - 有什么简单的方法可以找到当前的工作目录吗? C++

php - 在 While 循环中的表中拆分 ID

php - 使用 implode 函数读取复选框值

mysql - SQL 从连接表中选择具有最大值的行

count - 在Rails控制台中将Big Decimal转换为String

java - 如何访问 JAR 文件中的文件夹

php - Mysql 不接受表单输入 Laravel 5.2

php - 在以下场景中如何使复选框正常工作?