All TalkersCode Topics

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

Select Query In PHP With Where Clause

Last Updated : Mar 11, 2024

Select Query In PHP With Where Clause

In this tutorial we will show you the solution of select query in PHP with where clause, today we are going to understand how to use select query in php MySQL with where clause.

As we know that select query is used to select data from MySQL and where is used to apply a condition on data that we get using select query.

Here, below we are going to give you some examples so that you see that how to use select query with where clause.

Step By Step Guide On Select Query In PHP With Where Clause :-

Here, as we say that select query is used to select data from MySQL and where is used to apply condition on data that we get using select query to show specific data which we want to get from database.

A general syntax of select query with where is given as:

SELECT * FROM table_name where id=1;

Here, * is used to select all columns, we can also specify the names of columns here instead of all and table_name is the name of table which we create in database here you see that we use where id = 1, it means that we apply condition that data must be showed or get where id is 1.

Now, let us see how we can use where clause with help of codes.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home page</title>
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
$query = "SELECT id, firstname, lastname FROM students where firstname = ‘sidhu’ ";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo " Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
} else {
    echo "0 results";
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, inside body as we can see that we use php tags and inside them we establish our connection with database and when our connection established successfully.
  6. Then our query runs in which we use select query on two columns whereas our table name is students. It means that from our student table only two columns are showed having name as firstname and lastname.
  7. Now, let us talk about where clause. As you can see that we use where firstname = sidhu. It means that the database showed on browser in which firstname must be sidhu.
  8. We can use where clause on all cloumns of table also like on lastname also and auto increment id which is a primary key.
  9. Here are many other method with help of which we can use select query with where. We will learn them one by one in next articles. If is true then our data got displayed on screen.
  10. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to use select query in php mysql with where clause.

I hope this tutorial on select query in PHP with where clause helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪