Old thread, but I thought I would post that I just accomplished this task and for such a simple thing, it was difficult finding anything on it. So in hopes of helping anyone else...here is some code from novice to novice.
I include this on my membersonly index page so that the htpasswd file is updated everytime the page is loaded. This pulls only the users I have that are listed in groups 5 or 6, myrow[2] is the username and myrow[3] is their password.
<?php
$fp = @fopen ("/home/website/www/membersonly/.htpasswd", "w+");
$db=mysql_connect(localhost, USER, PASS);
mysql_select_db(DATABASE, $db);
$result=@("select * from user where ((usergroupid='5') or (usergroupid='6'))", $db);
while ($myrow=@mysql_fetch_row($result)) {
$user = $myrow[2];
$pass = crypt($myrow[3]);
$userpass = ("$user:$pass\n");
@fwrite ($fp,"$userpass");
}
@fclose ($fp);
?>
Works great, though may be messy code 😃
Hope this posts ok, my first post here.
Good luck.