PHP 基础教程
PHP 高级教程
PHP & MySQL DATABASE
PHP 示例
PHP 参考手册

PHP MySQL WHERE 子句

在本教程中,您将学习如何使用 PHP 根据特定条件从 MySQL 数据库表中选择记录。

过滤记录

WHERE 子句仅用于提取满足指定条件的记录。

WHERE 子句的基本语法可以通过以下方式给出:

SELECT column_name(s) FROM table_name WHERE column_name operator value

让我们使用 SELECT 语句中的 WHERE 子句进行 SQL 查询,然后我们将通过将其传递给 PHP mysqli_query() 函数来执行此查询以获取过滤后的数据。

假设我们在 demo 数据库中有一个 persons 表,其中包含以下记录:

+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    | peterparker@mail.com |
|  2 | John       | Rambo     | johnrambo@mail.com   |
|  3 | Clark      | Kent      | clarkkent@mail.com   |
|  4 | John       | Carter    | johncarter@mail.com  |
|  5 | Harry      | Potter    | harrypotter@mail.com |
+----+------------+-----------+----------------------+

以下 PHP 代码从 persons 表中选择 first_name='john' 的所有行:

示例

Procedural Object Oriented PDO
Download
<?php
/* 尝试 MySQL 服务器连接。 假设您正在运行 MySQL
具有默认设置的服务器(用户 'root' 没有密码) */
$link = mysqli_connect("localhost", "root", "", "demo");
 
// 检查连接
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 
// 尝试选择查询执行
$sql = "SELECT * FROM persons WHERE first_name='john'";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "<table>";
            echo "<tr>";
                echo "<th>id</th>";
                echo "<th>first_name</th>";
                echo "<th>last_name</th>";
                echo "<th>email</th>";
            echo "</tr>";
        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['first_name'] . "</td>";
                echo "<td>" . $row['last_name'] . "</td>";
                echo "<td>" . $row['email'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        // 关闭记录集
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
 
// 关闭连接
mysqli_close($link);
?>
<?php
/* 尝试 MySQL 服务器连接。 假设您正在运行 MySQL
具有默认设置的服务器(用户 'root' 没有密码) */
$mysqli = new mysqli("localhost", "root", "", "demo");
 
// 检查连接
if($mysqli === false){
    die("ERROR: Could not connect. " . $mysqli->connect_error);
}
 
// 尝试选择查询执行
$sql = "SELECT * FROM persons WHERE first_name='john'";
if($result = $mysqli->query($sql)){
    if($result->num_rows > 0){
        echo "<table>";
            echo "<tr>";
                echo "<th>id</th>";
                echo "<th>first_name</th>";
                echo "<th>last_name</th>";
                echo "<th>email</th>";
            echo "</tr>";
        while($row = $result->fetch_array()){
            echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['first_name'] . "</td>";
                echo "<td>" . $row['last_name'] . "</td>";
                echo "<td>" . $row['email'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        // Free result set
        $result->free();
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
 
// 关闭连接
$mysqli->close();
?>
<?php
/* 尝试 MySQL 服务器连接。 假设您正在运行 MySQL
具有默认设置的服务器(用户 'root' 没有密码) */
try{
    $pdo = new PDO("mysql:host=localhost;dbname=demo", "root", "");
    // 设置 PDO 错误模式为异常
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
    die("ERROR: Could not connect. " . $e->getMessage());
}
 
// 尝试选择查询执行
try{
    $sql = "SELECT * FROM persons WHERE first_name='john'";  
    $result = $pdo->query($sql);
    if($result->rowCount() > 0){
        echo "<table>";
            echo "<tr>";
                echo "<th>id</th>";
                echo "<th>first_name</th>";
                echo "<th>last_name</th>";
                echo "<th>email</th>";
            echo "</tr>";
        while($row = $result->fetch()){
            echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['first_name'] . "</td>";
                echo "<td>" . $row['last_name'] . "</td>";
                echo "<td>" . $row['email'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        // Free result set
        unset($result);
    } else{
        echo "No records matching your query were found.";
    }
} catch(PDOException $e){
    die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
 
// 关闭连接
unset($pdo);
?>

过滤后的结果集将如下所示:

+----+------------+-----------+---------------------+
| id | first_name | last_name | email               |
+----+------------+-----------+---------------------+
|  2 | John       | Rambo     | johnrambo@mail.com  |
|  4 | John       | Carter    | johncarter@mail.com |
+----+------------+-----------+---------------------+
Advertisements