PHP – MySQL database connection
We already know that the PHP Scripting is used to develop a web based application. But only PHP we can develop just static web site. Which is just information site for any industry or company, college.
If we want to develop a dynamic web application in PHP then we must have to use database for store, retrieve and updating information.
When we decide to create a dynamic website in PHP, then first step to create a database and Table in it, which we already learn in out previous PHP tutorial.
After creating a database and table in MySQL establish a connection between PHP application and MySQL Database.
PHP- MySQL database connection
Each database has unique username and password to connect database and manipulate.
$username = “your_name”;
$password = “your_password”;
$database=”your_database”;
$hostname = “localhost”;
Here the “Your_name” and “your_password” replaced by the MySQL username and password.
mysql_connect()
The mysql_connect() PHP function is used to connect MySQL database.
Now, Connect your PHP script to MySQL database use mysql_connect function.The first thing to do is connect to database , the mysql_connect function is used to connect MySQL database. The mysql_connect function returns a resource which is pointer to the database connection and it is also called database handle.
mysql_connect($hostname, $username, $password)
above code used to connect MySQL database server”localhost’ to PHP script. After creating a connection between PHP script and MySQL server select database you wish to use.
@mysql_select_db
The @mysql_select_db() function is used to select database for use.
@mysql_select_db($database) or die(“Unable to select database”);
Above line select the database stored in the $database variable. If the database does not connect properly then it will generate error message.
mysql_close()
The mysql_close() PHP function is used to close the MySQL Connection.
Now after creating database connection to MySQL finally close the connection using mysql_close() function.
mysql_close();
Example to connect MySQL Database using mysql_connect()
<?php
$username = "user_name";
$password = "user_password";
$hostname = "localhost";
$database="School";
//connection to the mysql database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db($database,$dbhandle)
or die("Could not select database School");
//execute the SQL Statement
$result = mysql_query("SELECT id, Name, City FROM StudentMst");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "ID:".$row{'id'}." Name:".$row{'name'}."City: ". $row{'city'}."<br>";
}
//close the connection
mysql_close($dbhandle);
?>
MySQLi Extension – (here ‘i’ means improved)
The PHP 5 and later version of PHP can use MySQLi Extension for establish a connection to MySQL database. The earlier version of PHP used MySQL extension for connectivity.
mysqli_connect()
mysqli_connect($hostname, $username, $password, $database )
PHP MySQL Connection Example – MySQLi Extension
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
$database="School";
//connection to the mysql database,
$dbhandle = mysqli_connect($hostname, $username, $password,$database )
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//execute the SQL Statement
$result = mysqli_query($dbhandle, "SELECT ID, Name, City FROM StudentMst" );
//fetch tha data from the database
while ($row = mysqli_fetch_array($result)) {
echo "ID:" .$row{'ID'}." Name:".$row{'Name'}." City: ". $row{'City'}."<br>";
}
//close the connection
mysqli_close($dbhandle);
?>
The result of above PHP mysql example is :