This is the very basic step, needed to work with MySQL databases. Before trying to do any actions on the database itself, such as: creating tables, querying rows, dropping tables or else, you have to make your PHP script connect to the MySQL database.
$host = "mysql_host"; $user = "mysql_user"; $pass = "mysql_password"; $db = "mysql_databasename"; $connect = mysql_connect($host, $user, $pass) or die(mysql_error()); $dbselect = mysql_select_db($db) or die(mysql_error());
$host – Your database hostname. The default is “localhost” but some hosts use a separate servers for the databases and files.
$user – The MySQL username. You need to create one through a web-based SQL interface if none exists (e.g through phpMyAdmin)
$pass – The login password associated with the above username.
$db – The name of the database you want to select. You are required to select one that your PHP script will work with later on.
Of course, for performance improvement, it is recommended that you close your database connections when you don’t need them anymore. You can use the following script:
$close = mysql_close($connect) or die(mysql_error());