PHP MySQL Where Clause

Para sa pagpili ng datos na tumutugma sa tiyak na kondisyon, magdagdag ng WHERE clause sa SELECT statement.

WHERE clause

Para sa pagpili ng datos na tumutugma sa tiyak na kondisyon, magdagdag ng WHERE clause sa SELECT statement.

Gramata

SELECT column FROM table
WHERE column operator value

Mga operator na maaaring gamitin kasama ang WHERE clause:

Operators Description
= Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
BETWEEN Between a range of values
LIKE Search for a matching pattern

Note:SQL statements are case-insensitive. WHERE is equivalent to where.

To make PHP execute the above statements, we must use the mysql_query() function. This function is used to send queries and commands to the SQL connection.

Example

The following example will select all rows from the 'Persons' table where FirstName='Peter':

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons
WHERE FirstName='Peter'");
while($row = mysql_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br />";
  }
?>

The output of the above code is:

Peter Griffin