Display Limited Query from Database in php - Web Development and Online Earning Tutorials

Latest

Like Box

BANNER 728X90
BANNER 728X90

Tuesday, February 13, 2018

Display Limited Query from Database in php

Display Limited Query from Database in php

Display Limited Query from Database in php
Display Limited Query from Database in php

Hi Friends Today we are going to learn how to display limited rows from data base using php..

=>Database and Table:
  Database Name: test
 Table name: user

Table Structure for table user:

CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `email` varchar(50) NOT NULL,
  PRIMARY KEY(id)
) ENGINE=MyISAM AUTO_INCREMENT=52 DEFAULT CHARSET=latin1;

Dumping Data for Table User:

INSERT INTO `user` (`id`, `username`, `email`) VALUES
(NULL, 'ANJAN KUMAR', 'Anjankumardhali@gmail.com'),
(NULL, 'Supriya Gain', 'SupriyaStar@Yahoo.com'),
(NULL, 'Supriya Gain', 'SupriyaStar@Yahoo.com'),
(NULL, 'ANJAN', 'biswasshiuli608@gmail.com'),
(NULL, 'ANJAN', 'biswasshiuli608@gmail.com'),
(NULL, 'ANJAN', 'biswasshiuli608@gmail.com'),
(NULL, 'Supriya Gain', 'SupriyaStar@Yahoo.com'),
(NULL, 'ANJAN KUMAR', 'Anjankumardhali@gmail.com'),
(NULL, 'ANJAN', 'biswasshiuli608@gmail.com'),
(NULL, 'ANJANBD', 'biswasshiuli608@gmail.com');

Create 3 files named 
1. connect.php
2. index.php
3. style.css

1. connect.php


<?php
$host="localhost"; //Your Host Name
$username="root"; //Your MySQL UserName
$pass=""; //Your MySQL PassWord
$dbname="test"; //Your Database Name
//Connect to Server
mysql_connect("$host","$username","$pass");
//Select Database
mysql_select_db("$dbname");
?>


2. index.php

<html>
<head>
<title>Display Limited Post or rows in PHP MySQL</title>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<h2>Displaying Limited ROWS/Records php mysql</h2>
<p>5 Records per Page</p>
<table>
  <tr>
    <th>ID No:</th>
 <th>Name:</th>
 <th>Email:</th>
  </tr>
  <?php
  include("connect.php");
  //We will show 5 ROWS per page
  $limit=5;
  $sql="SELECT*FROM user limit $limit";
  $result=mysql_query($sql);
  while($row=mysql_fetch_array($result)){
  ?>
  <tr>
    <td><?php echo $row['id'];?></td>
 <td><?php echo $row['username'];?></td>
 <td><?php echo $row['email'];?></td>
  </tr>
  <?php }?>
</table>
</html>

3. connect.php

td,th{
 padding:8px;
 border:1px solid green;
}
table{
 border-collapse:collapse;
 width:40%;
}
th{
 font-size:16px;
 padding:10px;
 color:white;
 background:#0033cc;
 border-radius:4px;
}
tr:nth-child(odd){background:lightgreen}
tr:nth-child(even){background:lightblue}

That's it Friend How to display limited rows from MySQL database.. Thank You...

No comments:

Post a Comment

Thank You for Your Comment