Wednesday, October 23, 2013

The best way we can Search data from Database in PHP

// Firstly Make a file searchdata.php



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Result</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>


<h1 align="center">Search Result</h1>



<?php
error_reporting(0);

//Connect To Database
mysql_connect("localhost","root","");
mysql_select_db("college");


//Get Keyword From Text Box
$searchterm = $_POST['searchterm'];

//This is only displayed if they have submitted the form with keyword
if(!empty($searchterm))
{
//Now we search for our search term, in the field the user specified
$selecta=mysql_query("select * from student where name like '%".$searchterm."%' ");

//And we display the results
while($rowa=mysql_fetch_array($selecta))
{
?>
<b style="color:#F00; font-size:14px; ">College Name:</b>
            <a href="#" style="text-decoration:none">
            <b style="color:#0472fe; font-size:15px; "><?php echo $rowa['name']; ?></b></a>
            <br />
         
            <b style="color:#F00; font-size:14px; ">Class:</b>
            <a href="#" style="text-decoration:none">
            <b style="color:#0472fe; font-size:15px; "><?php echo $rowa['class']; ?></b></a>
            <br />
         
            <b style="color:#F00; font-size:14px; ">rollno:</b>
            <a href="#" style="text-decoration:none">
            <b style="color:#0472fe; font-size:15px; "><?php echo $rowa['rollno']; ?></b></a>
            <br />
<?php
}
}
else
{
 //If they did not enter a search term we give them an error
echo "<h1 align=center>Please Enter a keyword to find! </h1>";
}
?>

</body>
</html>



// Make a file index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="searchdata.php" method="post" >
<h1>Enter College Name</h1>
<input type="text" name="searchterm"
style="height:30px; width:270px; border-radius:2px 0 0 2px; border:none; font-size:20px; color:#808080; background:#999" />
<input type="submit"/>
</form>
</body>
</html>


// Database


--
--  Create Database Name: `college`
--

-- --------------------------------------------------------

--
-- Table name -  `student`
--

CREATE TABLE IF NOT EXISTS `student` (
  `name` varchar(11) NOT NULL,
  `class` varchar(22) NOT NULL,
  `rollno` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `student`
--

INSERT INTO `student` (`name`, `class`, `rollno`) VALUES
('a', '1', 1),
('b', '2', 2);






No comments: