Re: Stuck! GDBM not listing my whole database!?
| From: | Rasmus Lerdorf | Date: | Thu, 06 Jul 2000 05:51:21 +0000 |
| Subject: | Re: Stuck! GDBM not listing my whole database!? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-5163@lists.php.net to get a copy of this message | ||
On Wed, 5 Jul 2000, Cabel Sasser wrote:
> Hello PHP People,
>
> I'm new to this lovely language, and I'm growing quite attached. BUT.. ;)
>
> I have a GDBM database of around 50 or so key/value pairs. I know all
> these pairs exist because I can see them in the binary database file,
> AND I can read the values on them when I name the exact key I wish to
> read.
>
> HOWEVER, when I walk through the directory to list all the contents,
> it only lists 7 of them, and my listing loop quits early having
> reached the apparent "end".
>
> Why would GDBM and/or PHP not be listing the whole database?
>
> Here's my listing routine:
>
> $dbm = dbmopen("/www/db", "r");
> $key = dbmfirstkey($dbm);
> while ($key) {
> print $key."<BR>";
> $key = dbmnextkey($dbm, $key);
> }
>
> After the loop exits, $key = "". What's really weird is if I place a
> SECOND dbmnextkey() after the first, it'll move on happily.
>
> It's almost like it's actually finding a $key named "" (nothing),
> which is triggering the end of the loop early even though there's
> really more data to be discovered.
>
> dblist() returns "This is GDBM version 1.8.0, as of May 19, 1999."
>
> SO MANY THANKS in advance. This is the only thing that's tripping me
> up in my PHP adventure!
You probably do have a empty string key in there. Not entirely sure how
to deal with that. Perhaps try something like:
while(dbmexists($dbm,$key)) {
$key = dbmnextkey($dbm,$key);
}
Or just code around it. Make your loop quit only if you get 2 consecutive
false returns from dbmnextkey()
-Rasmus