how to fetching data in mysql using php

Today we are learn about data fetching in mysql and display data in a web page using php and html.

  1. create database in mysql with name “demo_db”.
  2. create table in this database with name “demo_tb”.
  3. create this three fields/columns in this table.
  • sno# [int (autoincrement)]
  • name [varchar(50)]
  • country [varchar(50)]

4.    insert dummy data in this table.


after completing this steps you create a php file with name “db_connection.php” and type following code in this file.


//database connection file
$host = “localhost”;    //your server host
$username = “root”;    //your server username
$password = “”;        //your server password
$db_name = “demo_db”   //your database name
//make connection with mysql
mysql_connect($host, $username, $password) or die(“Error in server connection!”);
//make connection with mysql database
mysql_select_db($db_name) or die(“Error in database connection!”);


after completing this you create another php file with name “datafetching.php”. and enter following code in this file.


include(“db_connection.php”);   //include database connection file.
$query = mysql_query(“select * from demo_tb”) or die(“Error in table query!”);
while($row = mysql_fetch_assoc($query)){
$sno = $row[“son#”];
$name = $row[“name”];
$country = $row[“country”];
<td><?php echo $sno; ?>
<td><?php echo $name; ?></td>
<td><?php echo $country; ?></td>


Thank you for reading this artical. if you’ve any type of peoblem or issues for understanding this code so please post your problem or issues in comment box or following our blog for more programming languages tutorials.
have a good day…:)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s