All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Search And Display Data From Database In PHP

Last Updated : Mar 11, 2024

How To Search And Display Data From Database In PHP

In this tutorial we will show you the solution of how to search and display data from database in PHP, for this let us understand an example, assume that we website of a college and queries. And we want to search someone’s name and answer to a question by finding a question, etc.

Then we search our requirements from the database and find the result. So, let us see how to search and display data from a database using PHP.

Step By Step Guide On How To Search And Display Data From Database In PHP :-

First of all, there are many ways to help which we can search and display data from databases using PHP.

Let us see a way from them with the help of the codes given below.

<!DOCTYPE html>
<html>
<head>
   <title> how to search and display data from database in PHP </title>
</head>
<body>
<h1>
 TalkersCode
</h1>
<h2>
 How to search and display data from a database in PHP
</h2>
<?PHP
    $connect= mysqli_connect("localhost","root","","database_name");
    $name = $_post['search'];
    $query = "SELECT * FROM TABLE_NAME
    WHERE first_name LIKE '%{$name}%' OR last_name LIKE '%{$name}%'";
    if (mysqli_connect_error())
      {
      echo "Failed to connect to MySQL: ". mysqli_connect_error();
      }
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result))
{
        echo $row['first_name']. " " . $row['last_name'];
        echo "<br>";
}
    mysqli_close($connect);
    ?>
</body>
</html>
  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, as we see above that we create a database connection first inside this we have to give the database name after this we have to give the connection to the table from which we want to retrieve data.
  7. Now, this all is done with help of query like. Yes, like is used to get the data matched that we fill or give to found. and when the query runs successfully. This helps us to retrieve data matched with the letters we give.

Conclusion :-

At last, in conclusion, here we can say that with the help of this article we can understand how to search and display data from a database in PHP.

I hope this tutorial on how to search and display data from database in PHP helps you and the steps and method mentioned above are easy to follow and implement,

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪