Categories
PHP

PHP Tutorial to fetch data from MySQL database

Many of you asked that how can we retrieve or fetch data from MySQL database using PHP. Yeah we know it’s an easy job by you PHP expert, but just have a look at the standard method you will find something interesting in this PHP tutorial.

Let’s just start with creating a sample database in MySQL, Create a table in database and insert some records in this table. For database creation and Insertion, We as expert in PHP have recently shared an article on Registration form in PHP with database connectivity  and by fetching more articles in mind we have built a tutorial on PHP responsive login form with MySQL database. We want to be connected with our readers you can simply contact us for any PHP related query.

On the same PHP working model let’s just follow the below steps:

Step 01: Create database

Open your phpmyadmin or MySQL. Create a database where you want to store your data and then we will use the same database to retrieve data.

Create database class;
mysql> use class
mysql> create table student(id int,name varchar(40),email varchar(80),password varchar(40));
mysql>insert into student(id,name,email,password) values(1,'john','john.doe@gmail.com','12345');

Step 02: Create a file for database connection

We are going to create connect.php file where we will define all database configuration.

<?php
$hostname="localhost"; //local server name default localhost
$username="root";  //mysql username default is root.
$password="123456";       //blank if no password is set for mysql.
$database="class";  //database name which you created
$con=mysql_connect($hostname,$username,$password);
if(! $con)
{
die('Connection Failed'.mysql_error());
}
mysql_select_db($database,$con);
?>

Step 03: Create a file where you want to show your records

We have created a file called index.php. First include your connect.php file in to index.php so that all configuration can be include here. Now to fetch data I will use SELECT query by passing in Mysql_query() function.

 <?php     //start php tag

//include connect.php page for database connection
Include('connect.php');

$sql1= "select * from student";
$result=mysql_query($sql1)
or exit("Sql Error".mysql_error());
$num_rows=mysql_num_rows($result);

if($num_rows>0)
{

// show all records in a table

//create table
echo "<table>";
echo "<tr><th>Name</th><th>Email</th></tr>";

//now create a while loop where you can add all fetched rows in html table
while($row =mysql_fetch_array($result)
{
echo "<tr>";
echo "<td>".$row['name']."<td>";
echo "<td>".$row['email']."<td>";
echo "</tr>";
}

//now close the table out of loop
echo "</table>";
}

else
{
echo "No record found";
}
?>

Here we are able to fetch our data by using these simple code snippetsTo learn more more about PHP monolog and logging, read here (Our Sponsor).

By Akshma Sharma

Web & PHP Developer, with a zeal experience of web and application development on various platform like Android, iOS, PHP, MySQL, CMS, Wordpress, Joomla, Magento and Drupal.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.