PHP

How to fetch data from mysql using PHP

In previous tutorial I explained how to insert data into mysql using PHP. Now in this tutorial i will explain how to fetch data from mysql using PHP.
We can fetch the data from mysql using following methods given below :

  • mysqli_fetch_array
  • mysqli_fetch_row
  • mysqli_fetch_assoc
  • mysqli_fetch_object

First Create a database with name demo. Now create a table with name data.
Here the strcuture of table

CREATE TABLE IF NOT EXISTS `data` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `contactno` bigint(20) NOT NULL,
  `gender` varchar(255) NOT NULL,
  `education` varchar(255) NOT NULL,
  `address` longtext NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

structure of data table

Code for mysql connection with php(config.php)

<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'demos');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Fetch data from mysql using mysqli_fetch_array

Fetch a result row as an associative array, a numeric array and also it fetches by both associative & numeric array.This function will actually return an array with both the contents of mysqli_fetch_row and mysqli_fetch_assoc merged into one. It will both have numeric and string keys.

<table width="100%"  border="1">
<tr>
<th>Sr no.</th>
<th>Name</th>
<th>Email</th>
<th>Contact no.</th>
<th>Gender</th>
<th>Education</th>
<th>Address</th></tr>
<?php
$query=mysqli_query($con,"select * from data");
$cnt=1;
while($row=mysqli_fetch_array($query))
{
?>
<tr>
<td><?php echo $cnt;?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['contactno'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['education'];?></td>
<td><?php echo $row['address'];?></td>
 </tr>
/* Now here both associative array and numeric array will work. */
<tr>
<td><?php echo $cnt;?></td>
<td><?php echo $row['1'];?></td>
<td><?php echo $row['2'];?></td>
<td><?php echo $row['3'];?></td>
<td><?php echo $row['4'];?></td>
<td><?php echo $row['5'];?></td>
<td><?php echo $row['6'];?></td>
 </tr>
 <?php
$cnt=$cnt+1;
 } ?>
</table>
Fetch data from mysql using mysqli_fetch_row

Mysqli_fetch_row fetch result row an numeric way. This function return a row where the value will come in the order as they are defined in the query, and the keys will span from 0 to one less than the number of columns selected.

Fetch data from mysql using mysqli_fetch_assoc()

Fetch a result row as an associative array.This function will return a row as an associative array where the column names will be the keys storing corresponding value.

<h3>How to Insert Data into MySql Using PHP(mysqi_fetch_assoc) </h3>
<hr >
<table width="100%"  border="1">
<tr>
<th>Sr no.</th>
<th>Name</th>
<th>Email</th>
<th>Contact no.</th>
<th>Gender</th>
<th>Education</th>
<th>Address</th></tr>
<?php
$query=mysqli_query($con,"select * from data");
$cnt=1;
while($row=mysqli_fetch_assoc($query))
{
?
<tr>
<td><?php echo $cnt;?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['contactno'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['education'];?></td>
<td><?php echo $row['address'];?></td>
 </tr>
 <?php
$cnt=$cnt+1;
 } ?>
</table>
Fetch data from mysql using mysqli_fetch_object()

Fetch a result row as an object.

<h3>How to Insert Data into MySql Using PHP(mysqli_fetch_object) </h3>
<hr >
<table width="100%"  border="1">
 <tr>
<th>Sr no.</th>
<th>Name</th>
<th>Email</th>
<th>Contact no.</th>
 <th>Gender</th>
<th>Education</th>
<th>Address</th></tr>
<?php
$query=mysqli_query($con,"select * from data");
$cnt=1;
while($row=mysqli_fetch_object($query))
{
?>
<tr>
<td><?php echo $cnt;?></td>
<td><?php echo $row->name;?></td>
<td><?php echo $row->email;?></td>
<td><?php echo $row->contactno;?></td>
<td><?php echo $row->gender;?></td>
<td><?php echo $row->education;?></td>
<td><?php echo $row->address;?></td>
 </tr>
 <?php
$cnt=$cnt+1;
 } ?>
</table>
View Demo
Download Source Code (How to fetch data from mysql using PHP)
Size: 27.6 KB
Version: V 2.0

Recommended Tutorials for you


Related posts

How to get current page URL in php

Anuj Kumar

PHP Variables

Anuj Kumar

How to upload and validate a image in php

Anuj Kumar

Useful functions for arrays in PHP

Anuj Kumar

JQuery Ajax Tutorial – Learn About Developing Effective Next Generation Web Application

Anuj Kumar

PHP – Data Types

Anuj Kumar

Understanding of php.ini

Anuj Kumar

How to get Browser Information In PHP

Anuj Kumar

Captcha Image Verification

Anuj Kumar

2 comments

Ankush December 20, 2018 at 12:55 pm

good tutorials ….Can you tell one thing where php tag is closed in between while loop?
Thanks sir

Reply
Anuj Kumar December 20, 2018 at 4:49 pm

While(){
——-
——-
}

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

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