How to Search data in a table

index.php
——————————————
<form action=”search.php” method=”GET”>
<input type=”text” name=”query” />
<input type=”submit” value=”Search” />
</form>

==============================================
======search.php==============================
<?php
$query = $_GET[‘query’];
$min_length = 3;

if(strlen($query) >= $min_length){
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query(“SELECT * FROM articles
WHERE (`title` LIKE ‘%”.$query.”%’) OR (`text` LIKE ‘%”.$query.”%’)”) or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

while($results = mysql_fetch_array($raw_results)){

echo “<p><h3>”.$results[‘title’].”</h3>”.$results[‘text’].”</p>”;

}

}
else{
echo “No results”;
}

}
else{
echo “Minimum length is “.$min_length;
}
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s