PHP MySQL Select Where

The SELECT statement is used to fetch data from a database. The WHERE clause is used to filter data based on specific conditions. The WHERE clause can be combined with the SELECT statement to fetch only specific data from the database.

Here is the basic syntax of the SELECT statement with WHERE clause:

SELECT column1, column2, ...
FROM table_name
WHERE condition;

In the above syntax:

  • column1, column2, ... are the columns you want to fetch data from.
  • table_name is the name of the table you want to fetch data from.
  • condition is a logical expression that determines what data to retrieve.

Example

Consider a table named customers with the following data:

idnameemailcountry
1John[email protected]USA
2Jane[email protected]UK
3Alice[email protected]France

Here is an example of how to use the SELECT statement with WHERE clause to retrieve only specific data from the customers table:

SELECT name, email
FROM customers
WHERE country = 'USA';

The above SELECT statement will return only the rows where the country column is equal to USA. The output will be:

nameemail
John[email protected]

Multiple Conditions

You can also use multiple conditions in the WHERE clause. For example, you can retrieve all customers from the customers table who live in the USA and have an email address that ends with example.com.

SELECT name, email
FROM customers
WHERE country = 'USA'
AND email LIKE '%example.com';

The above SELECT statement will return the following output:

nameemail
John[email protected]

You can use logical operators such as AND and OR to combine multiple conditions in the WHERE clause.

Practice Your Knowledge

What is the use of the WHERE clause in MySQL in PHP?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?