Searching Data in Database Using PHP

index.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form action="search.php" method="get" >
<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>
</form>
</body>
</html>

search.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
    mysql_connect("localhost", "root", "") 
    mysql_select_db("tutorial_search") or die(mysql_error());
?>
<?php
    $query = $_GET['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'>";
                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;
    }
?>
<td width="30px;">
</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:

PHP coding for seacrching data in database

No comments:

Post a Comment