.


Divya

PHP MySQLi WHERE Clause Example


To select, update, delete specific record from the table where clause is used.

SQL Where Clause use for Select

SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;

The WHERE clause specifies which record or records that should be selet. If you omit the WHERE clause, all records will be select.

Example

<?php
$servername='localhost';
$username='root';
$password='';
$dbname = "my_db";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
$result = mysqli_query($conn,"SELECT * FROM students where First_name='Divya' ");
?>
<!DOCTYPE html>
<html>
<head>
<title> Retrive data</title>
</head>
<body>
<table>
<tr>
<td>id</td>
<td>First Name</td>
<td>Last Name</td>
<td>City</td>
<td>Email id</td>
</tr>
<?php
$i=0;
while($DB_ROW = mysqli_fetch_array($result)) {
?>
<tr class="<?php if(isset($classname)) echo $classname;?>">
<td><?php echo $DB_ROW["id"]; ?></td>
<td><?php echo $DB_ROW["First_name"]; ?></td>
<td><?php echo $DB_ROW["Last_name"]; ?></td>
<td><?php echo $DB_ROW["City_name"]; ?></td>
<td><?php echo $DB_ROW["Email"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>

SQL Where Clause use for Delete

DELETE column_name,column_name
FROM table_name
WHERE column_name operator value;

The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted.

Example

<?php
$servername='localhost';
$username='root';
$password='';
$dbname = "my_db";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
$result = mysqli_query($conn,"DELETE * FROM students where First_name='Divya' ");
?>

SQL Where Clause use for Update

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

The WHERE clause specifies which record or records that should be Update. If you omit the WHERE clause, all records will be Update.

Example

<?php
$servername='localhost';
$username='root';
$password='';
$dbname = "my_db";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
$result = mysqli_query($conn,"UPDATE students set First_name='Divya' where id=2");
?>


.