Once you’ve created a database via your hosting control panel, you can connect to it using PHP.
PHP provides a set of instructions for accessing MySQL databases.
Here’s an example connection. The sample database details are:
- Database name: “db_test”
- Database admin user: “user_test”
- Database password: “test_pass”
<html> <head> <title>PHP Example</title> </head> <body> <?php function Connect() { if (!($connection=mysql_connect("localhost","user_test","test_pass"))) { echo "Error connecting to the database."; exit(); } if (!mysql_select_db("db_test",$connection)) { echo "Error selecting the database."; exit(); } return $connection; } $connection=Connect(); echo "The connection to the database has been successfully established.<br>"; // Database access statements should go here. mysql_close($connection); //closes the connection ?> </body> </html>
When the mysql_connect statement is executed, it creates a link between the database and the PHP page. This link will be used for queries made to the database.
After we finish using the link to the database, we release it with the mysql_close statement to avoid tying up the connection.