Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Tuesday 25 March 2014

PHP Database Connection With MySQL

Hello friends, this article is for php beginners who want to know how to connect to MySQL database using PHP code.

Here is your answer, I am explaining you step by step tutorial of how to make connection to MySQL database in PHP (Hypertext Preprocessor) language.

PHP Database Connectivity With MySQL
MySQL - PHP Database Connectivity

First of all you have to use mysql_connect() function. And mysql_connect() function contains host-name, username, and password.

If you have not set username then default is 'root'. If you have not set password then it is blank / empty. And hostname is 'localhost'.


Following PHP code explains how to connect with MySQL database:

$con = mysql_connect('localhost','username','password');

If you not set any username or password then write following PHP code:

$con = mysql_connect('localhost','root','');

mysql_connect() function returns a resource which is a pointer to a database connection.

After establishing connection to MySQL, now we have to select database.

To select database, we have to use mysql_select_db() function.

mysql_select_db() function takes two parameter as database name and connection to MySQL.

Consider following PHP code to select database from MySQL:

mysql_select_db('database_name', $con);

$con is a variable created above using mysql_connect() function.

After mysql_connect() and mysql_select_db() functions, now you can execute any query using mysql_query() function.

mysql_query() function takes SQL query as parameter.

mysql_query() function returns a resource that contains the results of the query called result set.

To fetch data from result set, we have to use mysql_fetch_array() function and this function takes one parameter result set.

To fetch records, we have to use while loops as illustrated in below code:

$rs = mysql_query('SELECT id, name, city FROM stud');
while( $row = mysql_fetch_array($rs) )
{
   echo $row['id'];
   echo $row['name'];
   echo $row['city'];
}

Using mysql_query() function, you can execute any query like update, delete, insert, select, etc.

What if query is not executed? You can write die() function as described below:

$rs = mysql_query('SELECT * FROM stud') or die('Query is not executed');

You can write die() function with mysql_connect(), mysql_select_db(), etc functions.

Below is a complete PHP MySQL database connectivity code:

<?php
  $con = mysql_connect('localhost','root','');
  mysql_select_db('mydb', $con);
  $rs = mysql_query('SELECT id,name,city FROM stud') or die('Query is not executed');
  while($row = mysql_fetch_array($rs))
  {
     echo $row['id'];
     echo $row['name'];
     echo $row['city'];
  }
?>

NOTE: You must have 'mydb' database in MySQL and contains one table 'stud' and table fields are 'id', 'name', 'city'. And must have some data inserted before you execute above code.

0 comments :

Post a Comment