Learn PHP : Making a connection to MySQL database

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.

$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';

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

// This is an example of connection.php
$dbhost = 'localhost';
// HOST OF DATABSE generally its localhost
$dbuser = 'root';
$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');

Author: Param Lowe

SixthSense India ! Q. What is sixthsense? A. Sixth Sense is evolved or developed sensory power of mind. I am the real living example of SixthSense in humans. Wanna learn how to develop sixth sense? Join Param Lowe's online webcast starting April 2012. The fee of joining is USD 9045.00. send email to sixthsense@parmlowe.com

Leave a Reply

Your email address will not be published. Required fields are marked *