you could create a .sql file and run it via php which would create all the necessary tables and such, but Sam is right. The database must be created manually first before the tables can be added.
As for the connection piece, I use the following stock code all over my scripts:
$sql = "SELECT DISTINCT added FROM validated ORDER BY added DESC";
$connection = mysql_connect("host", "user", "pass")
or die ("Couldn't connect to server.");
$db = mysql_select_db("databasename", $connection)
or die ("Couldn't select database.");
$sql_result = mysql_query($sql)
or die("Couldn't execute query.");
if (!$sql_result) {
echo "No records found!";
} else {
continue with script...
As with anything, there is always more than one way to accomplish the same goal.