All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Simple Search Code In PHP With Demo

Last Updated : Mar 11, 2024

Simple Search Code In PHP With Demo

In this tutorial we will show you the solution of simple search code in PHP with demo, here simple search code means there are many rows inside a table that are more than 10000 and we have to search for a name.

For this, we have to create a simple search box, which helps us to access the data which we want. So, to do this let us understand the example given below.

Step By Step Guide On Simple Search Code In PHP With Demo :-

There are many ways with help of which we can create a search code in PHP.

You can directly create a table and insert data in it manually and create a search box to search something or we can also get the help of a database.

In this article, we are going to get the help of a database. So, let us see how to do this. Before starting this there must be a database and a table already created with some data.

<!DOCTYPE html>
   <title> simple search code in PHP with demo </title>
 Simple search code in PHP with demo
<form class="form-inline" method="POST" action="index.php">
<input type="text" class="form-control" placeholder="Search here..." name="keyword" required="required" value="<?php echo isset($_POST['keyword']) ? $_POST['keyword'] : '' ?>"/>
<span class="input-group-btn">
<button class="btn btn-primary" name="search"><span class="glyphicon glyphicon-search"></span></button>
<br />
$keyword = $_POST['keyword'];
<hr style="border-top:2px dotted #ccc;"/>
$conn = mysqli_connect('localhost', 'root', '', 'database_name') or die(“Database connection failed”);
$query = mysqli_query($conn, "SELECT * FROM `table_name` WHERE `title` LIKE '%$keyword%' ORDER BY `title`") or die(mysqli_error());
while($fetch = mysqli_fetch_array($query)){
<a href="data.php?id=<?php echo $fetch['blog_id']?>"><h4><?php echo $fetch['title']?></h4></a>
<p><?php echo substr($fetch['content'], 0, 100)?>...</p>
  1. As, here we see that in the above example, we show you how HTML and PHP codes are used.
  2. Here, first of all, we create a basic structure of HTML, in which we use <!DOCTYPE html> which defines the type of document. And next one is our HTML tags. These tags are paired tags and all the data regarding HTML is written inside these tags.
  3. After we use our head tag which is again paired tag and contains the title and meta information of the webpage. The data written inside the head is not shown on the webpage.
  4. Now, next is our title tag which defines the title of the webpage. The tag which has its closing tag is known as a paired tag. So, this is again a paired tag.
  5. Now, next is the body which is the main tag of HTML. The data which we have written inside the body is shown on the webpage. Mostly all tags which are helpful to show data or information on the screen are written under the body tag.
  6. Here, in the above codes we see that we create a database connection and find data on basis of the text which we fill inside the input type text field. With the help of a like query, we can fetch the required data from the database and it will show us.

Conclusion :-

At last, in conclusion, here we can say that with the help of this article we can understand how to use simple search code in PHP with help of a demo.

I hope this tutorial on simple search code in PHP with demo helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪