Probably, if the database didn't exist then it wouldn't select it so the CREATE TABLE would fail.
Try changing the $sql
query to:
$sql = "CREATE TABLE IF NOT EXISTS `" . $dbname . "`.`users` (
`id` int(11) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`email` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
EDIT: As others have suggested, another possible solution would be to call mysql_select_db (or your your favourite newer alternative to the mysql_* functions) but this involves more data transfer to the database so using this method will be slightly more efficient. Although that efficiency will be negligible in this scenario probably!