PHP MySQL The Where Clause

By | September 27, 2022

The WHERE clause is used to filter records.

The WHERE clause

The WHERE clause is used to extract only those records that fulfill a specified criterion.

Syntax

SELECT column_name(s)FROM table_nameWHERE column_name operator value

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Example

The following example selects 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 PersonsWHERE FirstName=’Peter'”); 
while($row = mysql_fetch_array($result)) 

echo $row[‘FirstName’] . ” ” . $row[‘LastName’]; 
echo “<br />”; 

?>

The output of the code above will be:

Peter Griffin
Category: PHP

Leave a Reply

Your email address will not be published. Required fields are marked *