PHP coding for searching data in database

     hi.... this is searching data in MySQL database  using PHP. LIKE  keyword to used searching related data to search .it searching a related data in database .this is a  very simple coding searching data.
    

syntax :


SELECT * FROM table_name WHERE (`column_name ` LIKE '%your_keyword%');

column_name- is  a  column name  ex: id,name .
LIKE - Keyword is used to select  related  data in database .
your_keyword - Enter your keyword . ex:php,java,hello

    1st  create a database   database name - tutorial_search .
    2nd crate table   table name - articles and create  column  title and text.

    finally run this program. 


Source Code:


<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tutorial_search") or die(mysql_error());
?>
<html>
<head>
<title>Search</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form method="post" >
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><input type="text" name="query" id="text" />&nbsp;</td>
<td><input type="submit" name="submit" id="search" value="Search" /></td>
</tr>
</table>
</form>

<?php

if(isset($_POST['submit']))
{
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length)
{
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
echo "<table border='0' width='300' align='center' cellpadding='1' cellspacing='1'>";
echo "<tr align='center' bgcolor='#002C40'>

<td height='35px' width='150px'>Title</td> <td>Author</td>

</tr>"
;
$raw_results =

mysql_query("SELECT * FROM articles WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')");
if(mysql_num_rows($raw_results) > 0)
{
while($results = mysql_fetch_array($raw_results))
{
echo "<tr align='center' bgcolor='#0f7ea3'>

<td height='25px'>"
.$results['title']."</td> <td>".$results['text']."</td>

</tr>"
;
}

}
else{
echo "<tr align='center' bgcolor='#6C0000'>

<td colspan='2' height='25px'>No results</td><tr>"
;
echo "</table>";
}
}
else{
echo "Minimum length is ".$min_length;
}}

?>

</body>
</html>


Style.css

body
{
background:url(bg.jpg);
}
table
{
margin:0 auto;
font-family:Tahoma, Geneva, sans-serif;
margin-top:200px;

}
#text
{
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
height:26px;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
border: 0px solid #003366;
-moz-box-shadow:
0px 1px 3px rgba(000,000,000,0.5),
inset 0px 0px 1px rgba(255,255,255,0.5);
-webkit-box-shadow:
0px 1px 3px rgba(000,000,000,0.5),
inset 0px 0px 1px rgba(255,255,255,0.5);
box-shadow:
0px 1px 3px rgba(000,000,000,0.5),
inset 0px 0px 1px rgba(255,255,255,0.5);
text-shadow:
0px -1px 0px rgba(000,000,000,0.7),
0px 1px 0px rgba(255,255,255,0.3);
}
#search {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #ffffff;
padding: 6px 20px;
background: -moz-linear-gradient(
top,
#1d8cb1 0%,
#0f7ea3);
background: -webkit-gradient(
linear, left top, left bottom,
from(#1d8cb1),
to(#0f7ea3));
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
border: 0px solid #003366;
-moz-box-shadow:
0px 1px 3px rgba(000,000,000,0.5),
inset 0px 0px 1px rgba(255,255,255,0.5);
-webkit-box-shadow:
0px 1px 3px rgba(000,000,000,0.5),
inset 0px 0px 1px rgba(255,255,255,0.5);
box-shadow:
0px 1px 3px rgba(000,000,000,0.5),
inset 0px 0px 1px rgba(255,255,255,0.5);
text-shadow:
0px -1px 0px rgba(000,000,000,0.7),
0px 1px 0px rgba(255,255,255,0.3);
}
#search:hover
{
background:#002C40;

}

 Download with database 


7 comments:

  1. In your search.php where's the closing tag for td width="30px;" ?

    ReplyDelete
    Replies
    1. it's no need ,,, Hezelle Grace Alcorano

      Delete
  2. Replies
    1. cool . plz check now .i'm simply now code....

      Delete
  3. please add profile photo and adding user option

    ReplyDelete
  4. and how to search only by id as roll no.(not other text)

    ReplyDelete