The benefit of choosing PHP as a web platform is its utility in dynamic and database driven web applications. It is fairly easy in PHP script to make a connection to MySQL database.
<?php $connection = mysql_connect("DataBase-Host", "DataBase-user","Password-of-databse") OR die('Error connecting to mysql'); //means do not execute code any further. $dbname = 'databasename'; mysql_select_db($dbname); ?>
There is one more better way to go with above code statement.
Good practice is to make a single file which can be called as and when it is required.
like you can save a file with a name e.g. connection.php with following code in it and can include it in any file to have a Database connection.
Here we will assign values to variables and it can be handy when you start coding more and more. You do not have to type data base connection script code every time just use include function and code will become part of that script.
e.g. <?php include “config.php”;?>
You can edit parameters to your own database values.
Save this file as config.php
<?php // This is an example of connection.php $dbhost = 'localhost'; // HOST OF DATABSE generally its localhost $dbuser = 'root'; // USER OF DATABASE $dbpass = 'password'; // PASSWORD of the Database User account $dbname = 'learnphp'; // Name of Databse $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); ?>