网站搜索

PHP MySQL WHERE 子句


如何在 XAMPP 中使用 PHP 从 MySQL 数据库中选择和过滤数据

在本指南中,我们将讨论如何在 XAMPP 堆栈中使用 WHERE 子句和使用 PHP 的 SELECT 命令根据特定条件从 MySQL 数据库中选择记录。

先决条件

确保您已按照以下指南所述在 XAMPP 堆栈中创建了数据库和表。

  • 在XAMPP中使用PHP创建MySQL数据库和表

出于演示目的,我在名为 “my_company” 的 MySQL 数据库中创建了一个名为 “sales” 的表,其架构和记录如下。

数据库架构和记录

使用 PHP 使用 WHERE 子句过滤 MySQL 表中的数据

WHERE 子句用于仅提取符合特定条件的记录。 WHERE 子句将通过采用运算符后跟值来检查条件。

WHERE 子句语法:

SELECT column1,column2,.,column n from table_name WHERE column_name operator value;

在哪里,

  • column_name - 应用条件的列。
  • operator - 用于检查条件。
  • value - 它是与表中存在的每一列值进行比较的字符串/数字值。

脚步

1. 指定服务器名称(例如 localhost)、数据库用户名(例如 root)、root 用户密码和数据库名称(例如 my_company)。在这里,我的 root 用户的密码为空。

2.使用mysqli_connect()函数建立连接。它将以服务器名、用户名和密码作为参数。

代码:

$connection = mysqli_connect($server_name, $user_name, $password, $database_name);

3.指定SQL查询以按条件从表中选择特定列或所有列的记录。在这一步中,我们可以指定 SQL 查询来从表中选择列放入变量中。

例如,我将使用名为 my_company 的数据库名称,并将其存储在名为 query 的变量中。表名称为 sales,包含三列。

代码:

$query = "SELECT column1,…. from sales where column_name operator value";

4.使用mysqli_query()函数将选定的结果存储到名为“final”的变量中。它将使用“连接”和“查询”作为参数。

代码:

mysqli_query($connection, $query);

5.使用mysqli_num_rows()函数从“final”变量中逐一获取行。之后,使用 mysqli_fetch_assoc() 函数迭代 while 循环来获取结果。它将采用“final”变量作为参数。

代码:

if (mysqli_num_rows($final) > 0) {
 //get the output of each row
  while($i = mysqli_fetch_assoc($final)) {
    echo $i["column1”],…………..;
  }
} else {
  echo "No results";
}

6.最后,使用mysqli_close()函数关闭连接。

代码:

mysqli_close($connection);

现在,让我们根据上述步骤编写一个示例 PHP 代码。

使用 WHERE 子句从 MySQL 数据库中选择数据的 PHP 代码

示例代码 1:

在此示例中,我们将从“sales”表中选择 id 值大于 4并在PHP页面中显示结果。

因此,通过将 column_name 指定为,我们的运算符将大于 (">"),并且值为 4 id。

在 /htdocs 文件夹下创建一个名为 select.php 的新文件,其中包含以下内容。

注意:如果您使用 Linux,htdocs 文件夹将位于 /opt/lampp/ 目录下。如果您使用的是 Windows,htdocs 通常位于 C:\xampp\ 文件夹中。

<?php
//specify the server name and here it is localhost
$server_name = "localhost";

//specify the username - here it is root
$user_name = "root";

//specify the password - it is empty
$password = "";

//specify the database name - "my_company"
$database_name = "my_company";

// Creating the connection by specifying the connection details
$connection = mysqli_connect($server_name, $user_name, $password, $database_name);

//sql query to select particular columns
//select all columns such that id is greater than 4
$query = "SELECT * from sales where id>4";

#get the result
$final = mysqli_query($connection, $query);

if (mysqli_num_rows($final) > 0) {
 //get the output of each row
  while($i = mysqli_fetch_assoc($final)) {
      //get all columns
    echo "id: " . $i["id"]. "  ----> name: " . $i["name"]."  ----> count: " . $i["count"]. "<br>";
  }
} else {
  echo "No results";
}

//close the connection
mysqli_close($connection);
?>

打开 Web 浏览器并将其指向 http://localhost/select.php URL。您可以看到id大于4的数据被选择,并且结果显示在浏览器窗口中。

使用 WHERE 子句从 MySQL 数据库中选择数据

示例代码2:

在此示例中,我们将从“sales”表中选择 name 为 的所有列>“鸡蛋”并在PHP页面中显示结果。

因此,通过将 column_name 指定为 name,我们的运算符将等于 ("="),并且值为“Eggs”。

<?php
//specify the server name and here it is localhost
$server_name = "localhost";

//specify the username - here it is root
$user_name = "root";

//specify the password - it is empty
$password = "";

//specify the database name
$database_name = "my_company";

// Creating the connection by specifying the connection details
$connection = mysqli_connect($server_name, $user_name, $password, $database_name);

//sql query to select particular columns
//select all columns such that name equals to Eggs
$query = "SELECT * from sales where name ='Eggs'";

#get the result
$final = mysqli_query($connection, $query);

if (mysqli_num_rows($final) > 0) {
 //get the output of each row
  while($i = mysqli_fetch_assoc($final)) {
      //get all columns
    echo "id: " . $i["id"]. "  ----> name: " . $i["name"]."  ----> count: " . $i["count"]. "<br>";
  }
} else {
  echo "No results";
}

//close the connection
mysqli_close($connection);
?>

打开 Web 浏览器并将其指向 http://localhost/select.php URL。您可以看到数据在 name ='Eggs' 处被选择并显示。

选择包含名称 Eggs 的列

示例代码3:

在此示例中,我们将从“sales”表中选择计数为 45 并在 PHP 页面中显示结果。

因此,通过将 column_name 指定为 count,我们的运算符将等于 ("="),并且值为 45。

<?php
//specify the server name and here it is localhost
$server_name = "localhost";

//specify the username - here it is root
$user_name = "root";

//specify the password - it is empty
$password = "";

//specify the database name
$database_name = "my_company";

// Creating the connection by specifying the connection details
$connection = mysqli_connect($server_name, $user_name, $password, $database_name);

//sql query to select particular columns
//select all columns such that count is 45
$query = "SELECT * from sales where count =45";

#get the result
$final = mysqli_query($connection, $query);

if (mysqli_num_rows($final) > 0) {
 //get the output of each row
  while($i = mysqli_fetch_assoc($final)) {
      //get all columns
    echo "id: " . $i["id"]. "  ----> name: " . $i["name"]."  ----> count: " . $i["count"]. "<br>";
  }
} else {
  echo "No results";
}

//close the connection
mysqli_close($connection);
?>

打开 Web 浏览器并将其指向 http://localhost/select.php URL。您可以看到在 count=45 处选择了数据,并显示了结果列。

选择包含计数 45 的列

结论

在本教程中,我们讨论了使用 PHP 和 WHERE 子句根据特定条件从 MySQL 数据库中选择数据的三种不同方法。第一个示例向您展示了如何使用数值“id”过滤数据。第二个和第三个示例分别说明了如何使用字符串值“name”和数值“count”来选择数据。

相关文章