The WHERE clause is used to filter records, which is to query the data under our specified conditions
Syntax
## Type Detailed explanation Basic syntax select field from table where where condition; Example select * from money where age = 29; Example description Query all results with age 29 in the MyGuests table Let us use the following example:
Example
The following example will select all FirstNames from the "MyGuests" table ='Mary' row:
<?php
header("Content-type:text/html;charset=utf-8"); //设置编码
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$con=mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (mysqli_connect_errno())
{
echo "连接失败: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM MyGuests
WHERE FirstName='Mary'");
while($row = mysqli_fetch_array($result))
{
echo $row['firstname'] . "----" . $row['lastname'] ."----".$row['email'];
echo "<br>";
}
?> Program execution result:
Use where condition to query all the data of FirstName='Mary' come out.
We now use phpadmin to add an Age field to the MyGuests table
The data in the table now looks like this:
Example
We now use the where clause to query those with Age less than 25:
<?php
header("Content-type:text/html;charset=utf-8"); //设置编码
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$con=mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (mysqli_connect_errno())
{
echo "连接失败: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM MyGuests
WHERE Age<25 ");
while($row = mysqli_fetch_array($result))
{
echo $row['firstname'] . "----" . $row['lastname'] ."----".$row['email']."----".$row['Age'];
echo "<br>";
}
?> Program running results:
You can also use the following conditions
Symbol Description ## > is greater than < Less than ## >= Greater than or equal to <= Less than or equal to ## != Not equal to ! = Equal to
Logical operators
## Symbol Description ## or Or and And Let’s take a look at an example of multiple conditions:
Example We query the data of 'firstname'='Julie' and Age=24
<?php
header("Content-type:text/html;charset=utf-8"); //设置编码
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$con=mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (mysqli_connect_errno())
{
echo "连接失败: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM MyGuests
WHERE Firstname='Julie' AND Age=24 ");
while($row = mysqli_fetch_array($result))
{
echo $row['firstname'] . "----" . $row['lastname'] ."----".$row['email']."----".$row['Age'];
echo "<br>";
}
?> Program running results:
<?php
header("Content-type:text/html;charset=utf-8"); //设置编码
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$con=mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (mysqli_connect_errno())
{
echo "连接失败: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM MyGuests
WHERE FirstName='Mary'");
while($row = mysqli_fetch_array($result))
{
echo $row['firstname'] . "----" . $row['lastname'] ."----".$row['email'];
echo "<br>";
}
?>
Course Recommendations
Courseware download
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~
Students who have watched this course are also learning