MySQL Where

We have seen MySQL SELECT command to fetch data from MySQL table. We can use a conditional clause called WHERE clause to filter out results. Using WHERE clause we can specify a selection criteria to select required records from a table.

If we wanted to select only certain entries of our table, then we would use the keyword WHERE.


SELECT * FROM table_name WHERE condition;

*You can use one or more tables separated by comma to include various condition using a WHERE clause. But WHERE clause is an optional part of SELECT command.
* You can specify any condition using WHERE clause.
* You can specify more than one conditions using AND or OR operators.
*A WHERE clause can be used along with DELETE or UPDATE MySQL command also to specify a condition.

In below example we will want to select all rows from employee table that have the string “learnwebdevleopment” in the “name” column.

PHP & MySQL Code:

// Make a MySQL Connection
mysql_connect("localhost", "dbusername", "dbpassword") or die(mysql_error());
mysql_select_db("test_db") or die(mysql_error());

// Get a specific result from the "employee" table
$select_q = "SELECT * FROM employee WHERE name='learnwebdevelopment'";
$select_r = mysql_query($select_q);
<table border="0" width="100%" align="center">
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_assoc( $select_r ))
// Print out the contents of each row into a table
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['age'];?></td>

Posted in MySQL