• Learn How to establish a connection between PHP and MySQL Database
  • Teach yourself How to make a PHP connection with MySQL Server
     

Tutorial of PHP Database Connectivity

How to connect to MySQL Database using PHP Script

Its a general query that How can we connect to MySQL Database within PHP WebPage.First of all you should have the server Host name or IP Address. Then another requirement is Database user name and password to establish a connection with MySQL.

So the initial data that you must need is..

  • Host Name
  • User Name
  • Password

Now we have a mysql_connect method method in PHP Language which is used to make a connection with database.It has three parameters as shown below.

	
	  $connection = mysql_connect($hostname, $username, $password);
		  

Here you get refrence of your MySQL connection in $con variable.Now you can check the avalibity of connection by following code..

	
		if (!$connection) {
die('Connection failed: ' . mysql_error());
}
else{
     echo "MySQL Database Connection established " ;
}

		  

MySQL Database Selection

After establishing a connection with MySQL Server our next step is to select the database through which you want to connect.Perticuler database should be exist at server.


		$database=”mydb”;
		$db = mysql_select_db($database, $connection);

		if (!$db) {
			die ('Database connection failed ' . mysql_error());
		}
		else {
			echo 'Database successfully connected!';
		}


	  

At last to close the connection we use mysql_close() method
mysql_close($connection);

So now you can go through further database SQL queries to retrieve your desired results.

Software Training & Solution Provider
258,Katewa Nagar,Jaipur,Rajasthan.
It's the near by location of Gurjar ki thadi, MANSAROVAR.
Ph: 9829708506 , 0141-4108506 , 08432830240, 8432706556

09829708506 , 08432830240