Select Query In PHP With Where Clause
Last Updated : Mar 11, 2024
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"> <head> <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> </head> <body> <?php $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"; } mysqli_close($conn); ?> </body> </html>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- 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.
- 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.
- 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.
- 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.
- We can use where clause on all cloumns of table also like on lastname also and auto increment id which is a primary key.
- 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.
- 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.