WHERE 子句用於過濾記錄,就是把我們指定條件的資料查詢出來
語法
類型 | # 說明 |
讓我們用下面的例子來看:
實例
#下面的實例將從"MyGuests" 表中選取所有FirstName ='Mary' 的行:
<?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>"; } ?>
程式運行結果:
#用where 條件把FirstName='Mary' 的資料全部查詢了出來。
我們現在用phpadmin將MyGuests 表裡面加入一個Age 的欄位
#現在表中的資料是這樣的:
實例
我們現在用where 字句把Age小於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>"; } ?>
程式運行結果:
#也可以使用下面的條件
符號 | 說明 |
> | 大於 |
< | # 小於 |
>= | 大於等於 |
<= | ##
!= | 不等於 |
= | o |
# 符號 | 說明 |
or | ## 或 |
## 或 |
and 且
我們來看看多個條件的範例:實例
#########我們將'firstname'='Julie' 且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>"; } ?>###程式執行結果:############下一節