Title: count() returning 1 when thousands of records
Tags: php;count;directory
Question: this count is onyl returning 1, when there are thousands of records in this folder.

```// 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/&gt;";

    closedir($handle);
}
```

Table Structure: id, title

Folder Structure: Sub folders in the main one

``` G:\TV
 G:\TV\24
 G:\TV\Family Guy
```

Comment: ofcourse because $dir="G:/TV" and its one

Comment: So there are thousands of records in the folder, and you're looping through each one of them and running 1 or possibly 2 queries for each file? That's going to absolutely wreck your database.

Comment: Hm, I think we need some more information to help you. Can you show us your folder structure and your database table layout please?

Comment: The value of $dir is string which is "G/TV";so it will return 1

Here is the accepted answer: You can't use ```count()``` on a folder. Try this one instead

```// 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/&gt;";

    closedir($handle);
}
```

```
  how about returning the number of new records inserted?
```

Check this out, just moved ```$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/&gt;";

    closedir($handle);
}
```

Comment for this answer: how about returning the number of new records inserted?
Comment for this answer: hmm i tried that before i asked and it didnt seem to work... il ltry again... it always returns 34 this way.

Here is another answer: You are calling ```count()``` on a string, not an array.

CodePad.


Here is another answer: Also a good alternative to this is using the php glob function. 
```$dir_path = "/full/directory/path/";```
```$count = count(glob($dir_path . "*"));```

This will also optimize your code !!


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:11/29/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><count><directory>', 'Title': 'count() returning 1 when thousands of records', 'ViewCount': 150, 'id': 8306613, 'user_AboutMe': '<p>PHP, HTML5, CSS3, MySQL... Currently Enrolled At Full Sail University, Studying Game Design.</p>\n', 'user_AccountId': 413109.0, 'user_DisplayName': 'rackemup420', 'user_Reputation': 1600.0, 'user_Views': 341.0, 'user_id': 786557.0}}