php mysql compare two rows in the same table - Web Development and Online Earning

Breaking

Web Development and Online Earning

Learn Web Development ,PHP MySQL,Codeigniter,AJAX,HTML CSS,Javascript,Jquery,Web Hosting,Online Earning,SEO,Link Building,Online Banking and More

test banner

Post Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

Wednesday, May 31, 2017

php mysql compare two rows in the same table

Compare Between Two Row form same Table using PHP MySQL

php mysql compare two rows
php mysql compare two rows

Hi Guys Today we learn How to Compare Table Data using PHP MySQL.
You Can Create Compare System Like GlobalFirePower Using The Below Codes.

What you will Need:

DataBase Creation:
Database Name: test
table Name: country
Use The Below SQL Codes to Create table country
CREATE TABLE IF NOT EXISTS `country` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(150) NOT NULL,
  `sort` varchar(150) NOT NULL,
  `army` varchar(50) NOT NULL,
  `aircraft` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

Now Use Below SQL Codes to Insert Some Row.
INSERT INTO `country` (`id`, `name`, `sort`, `army`, `aircraft`) VALUES
(1, 'Bangladesh', 'BD', '400000+', '220+'),
(2, 'India', 'IN', '1230000+', '1700+'),
(3, 'America', 'USA', '1300000+', '12000+'),
(4, 'United Kingdom', 'UK', '500000+', '500+'),
(5, 'Nepal', 'NP', '230000+', '100+'),
(6, 'Bulgaria', 'BG', 'Unknown', '50+');

Files Creation:
Create 3 files Named.
1.connect.php
2.index.php
3.style.css
php mysql compare two rows
php mysql compare two rows

Now copy the below Codes for Different files
connect.php
Creating Connection Between Server and Database.
<?php
$host="localhost"; //Host Name
$username="root"; //MySQL Username
$password=""; //MySQL password
$dbname="test"; //Database Name
//Connect MySQL Server
mysql_connect("$host","$username","$password");
//Select Database
mysql_select_db("$dbname");
?>

index.php
Add The Below HTML codes top of The Index Page to Display Comparison From.
<html>
<head>
<title>Compare Two Row from Table Like as GlobalFirePower</title>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<div id="form">
<h1>Compare Between Two Things Like as GlobalFirePower</h1>
<form method="POST" action="">
<label>Select a Country:</label><br/>
<input list="country" type="text" name="type1" required/>
<datalist id="country">
  <option value="America"/>
  <option value="India"/>
  <option value="Nepal"/>
  <option value="United Kingdom"/>
  <option value="Bangladesh"/>
</datalist>
<br/>
<label>Select a Country:</label><br/>
<input list="country" type="text" name="type2" required/>
<datalist id="country">
  <option value="America"/>
  <option value="India"/>
  <option value="Nepal"/>
  <option value="United Kingdom"/>
  <option value="Bangladesh"/>
</datalist>
<br/>
<input type="submit" name="submit" value="submit"/><br/>
</form>
</div>

Now add the Below Codes after HTML Codes to GET Result of Submited Data.
<?php
include("connect.php");
if(isset($_POST['submit'])){
 $country1=$_POST['type1'];
 $country2=$_POST['type2'];
//Query for First Country
$sql1="SELECT*FROM country WHERE name='$country1'";
$result1=mysql_query($sql1);
$row1=mysql_fetch_assoc($result1);
//Query for 2nd Country
$sql2="SELECT*FROM country WHERE name='$country2'";
$result2=mysql_query($sql2);
$row2=mysql_fetch_assoc($result2);
$countrow=mysql_num_rows($result2);

if($countrow==0){
 $error="Selected Country Does Not Exist";
}else{
?> 
<div id="left">
<table>
  <tr>
  <th>Name</th>
  <th>Sort Name</th>
  <th>Armed Force</th>
  <th>Aircraft</th>
  
  </tr>
  <tr>
 <td><?php echo $row1['name'];?></td>
 <td><?php echo $row1['sort'];?></td>
 <td><?php echo $row1['army'];?></td>
 <td><?php echo $row1['aircraft'];?></td>
 
</tr>
  <tr>
 <td><?php echo $row2['name'];?></td>
 <td><?php echo $row2['sort'];?></td>
 <td><?php echo $row2['army'];?></td>
 <td><?php echo $row2['aircraft'];?></td>
</tr>
</table>
</div>
<?php } } ?>
<div id="error"><?php if(isset($error)){echo $error;}?></div>
</html>

style.css
Now add some css to Design our Index Page
#form{
 width:800px;
 height:250px;
 background:#ddd;
 color:black;
}
input[type=text]{
 padding:7px;
 width:50%;
 border-radius:5px
}
input[type=text]:focus{
 border:2px solid black;
 padding:8px;
}
input[type=submit]{
 padding:5px;
 border-radius:5px;
 background:#080808;
 color:white;
}
label{
 font-size:20px;
 color:green;
 font-weight:bold;
}
#error{
 font-size:20px;
 color:green;
 font-weight:bold;
}

#left{
 background:#ddd;
 float:left;
 width:45%;
}
table th,td{
 border:1px solid red;
 background:lighter;
 text-align:left;
}
table{
 width:100%;
 border-collapse:collapse;
}
td{
 padding:8px;
 
}
th{
 padding:10px;
 font-weight:bold;
 font-size:20px;
 background:#080809;
 color:white;
}
tr:nth-child(odd){background:lightgreen}

Now You have Successfully Created PHP MySQL Data Compare System
Thank You for Visiting.

No comments:

Post a Comment

Thank You for Your Comment

Post Top Ad

Responsive Ads Here