PHP MySQL Connection

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()


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

The result of above PHP mysql example is :

php-mysql example

PHP MySQL Connection Example using MYSQLi Extension

 

Leave a Reply

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

9 + 5 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">