استفاده از شرط Where در PHP MySQL
- Previous Page MySQL Select
- Next Page MySQL Order By
要选择匹配指定条件的数据,请向 SELECT 语句添加 WHERE 子句。
WHERE 子句
要选择匹配指定条件的数据,请向 SELECT 语句添加 WHERE 子句。
语法
SELECT column FROM table WHERE column operator value
运算符可以与 WHERE 子句一起使用:
Operators | Description |
---|---|
= | Equal |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
BETWEEN | Between a range containing |
LIKE | Search for a matching pattern |
Note:SQL statements are case-insensitive. WHERE is equivalent to where.
To make PHP execute the above statement, we must use the mysql_query() function. This function is used to send queries and commands to the SQL connection.
Example
The following example will select 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 Persons WHERE FirstName='Peter'"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } ?>
The output of the above code:
Peter Griffin
- Previous Page MySQL Select
- Next Page MySQL Order By