PHP coding for searching data in database

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. 

Example:

SELECT * FROM Demo WHERE (`website ` LIKE '%crackerworld.in%'); 
Index.html

<html>
<head>
<title>Search</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form method="post" action="search.php" >
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><input type="text" name="query" id="text" /><input type="submit" name="submit" id="search" value="Search" /></td>
</tr>
</table>
</form>
</body>
</html>

search.php

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tutorial_search") or die(mysql_error());
if(isset($_POST['submit']))
{
$query = $_POST['query'];
?>
<html>
<head>
<title><?php echo $query;?></title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
$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' style='color:#FFF'>
<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>

Css

body{background:#FF8C55;font-family:Arial, Helvetica, sans-serif;}
table{margin:0 auto;margin-top:300px;}
#text{width:400px;padding:10px 0;font-size:22px;border-radius: 6px 0 0 6px;-moz-border-radius: 6px;-webkit-border-radius: 6px;border:1px solid #FFF;}
#search {width:100px; color: #ffffff;padding:10px 0;font-size:22px;background: #005680;border-radius:0 6px 6px 0;-moz-border-radius:0 6px 6px 0;-webkit-border-radius:0 6px 6px 0;border:none;}
#search:hover{background:#002C40;border:none;border-radius:0 6px 6px 0;-moz-border-radius:0 6px 6px 0;-webkit-border-radius:0 6px 6px 0;border:none;}

No comments:

Post a Comment