M. Niyazi Alpay
M. Niyazi Alpay
M. Niyazi Alpay

I've been interested in computer systems since a very young age, and I've been programming since 2005. I have knowledge in PHP, MySQL, Python, MongoDB, and Linux.

 

about.me/Cryptograph

PHP In-site Search Engine Construction

Sometimes we need a simple search engine within our websites to find topics. Let me explain this with a simple example:

HTML codes for the form that will perform the search:

<form action="result.php" method="get">
  <input type="text" name="searchquery" placeholder="Enter the keyword you want to search"><br>
  <input type="submit" value="Search">
</form>

Codes to be included in result.php:

<?php
$searchquery = @mysql_real_escape_string($_GET['searchquery']);
$resultquery = @mysql_query("SELECT * FROM topics WHERE title LIKE '%".$searchquery."%'" );
if(@mysql_num_rows($resultquery)>0){
  while($queryread=@mysql_fetch_array($resultquery)){
   echo $queryread['title'].'<br>';
  }
} 
else{  
echo 'Content Not Found'; 
} 
?>

We select the data from the table named topics with the query SELECT * FROM topics, but there is a condition here WHERE title to specify that it should take the data in the title column, but here another condition is specified LIKE is the command used to search from the database LIKE '%searched word%' format, we put our variable holding the query from the form between % signs, then we use a while loop to print the found results one below the other, you can do this in a more beautiful way, you can define it as a link or you can do it with a nice design, I explained it to you in the simplest way.

If you have any questions, you can reach me by commenting below the topic.

You may also want to read these

There are none comment

Leave a comment

Your email address will not be published. Required fields are marked *