Instead of using a .htaccess file, you can send authentication header requests with PHP. Send the headers before any text is outputted (unless you have PHP4 with output_buffering turned on in you php.ini file)
if(!isset($PHP_AUTH_USER))
{
Header("WWW-authenticate: basic realm=\"Complain\"");
Header("HTTP/1.0 401 Unauthorized");
}
else
{
// connect to database here
// $PHP_AUTH_USER is the username returned by the authentication box
$username = $PHP_AUTH_USER;
// $PHP_AUTH_PW is the password
$password = $PHP_AUTH_PW;
$query = "SELECT * FROM table WHERE username='$username' AND password='$password'";
}