PHP MySQL Where...LOGIN

PHP MySQL Where

The WHERE clause is used to filter records, which is to query the data under our specified conditions


Syntax

Basic syntax select field from table where where condition; Exampleselect * from money where age = 29; Example descriptionQuery 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:

4.png


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:

14.png

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:

1.png

You can also use the following conditions

## Type Detailed explanation
## > is greater than < Less than## >= <=
Symbol Description
Greater than or equal to
Less than or equal to
## != Not equal to ! = Equal to

Logical operators

## or Or and And Let’s take a look at an example of multiple conditions:
## Symbol Description

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:

Next Section
<?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>"; } ?>
submitReset Code
ChapterCourseware