Breaking

LightBlog

Saturday 4 July 2015

How to Connect a MySQL Database - PHP



Before You Can get Content out of your MySQL database. You must know how to establish a connection to MySQL from inside a PHP Script. PHP has a lot of inbuilt function you can use to manipulate database. We can use the below inbuilt function.
  • mysql_connect ( )   - Open the database to MySQL itself.
  • mysql_select_db ( ) – Select the database which you want to open.
  • mysql_close () – Close the database.
Syntax :--    mysql_connect( server name, username, password );
Step 1 – Open a connection to MySQL

  In MySQL we want to access data from database then first of all we have to connect to database. Without connection we cannot access the data form database.mysql_connect ( ) function is used to connect the database in PHP
<?php
$con=mysql_connect(“localhost”, “deep “, 12345);
if (!$con)
{
die(“could not be connect the database” . mysql_error());
}
echo “ connected successfully “;
?>
                                                 

Step 2 – Close the Database Connection
If we complete all the task related to the database  then we can close it in the point of security way. The mysql_close ( ) is used to close the database in PHP.

Eaxmple 

<?php
$con=mysql_connect(“localhost”, “deep “, 12345);
if (!$con)
{
die(“could not be connect the database” . mysql_error());
}
echo “ connected successfully “;
mysql_close ($con);
?>
Adbox