PHP MySQL LIKE 运算符

在本指南中,我们将讨论如何在 XAMPP 中使用 PHP 在特定数据库中使用 MySQL LIKE 运算符和 WHERE 子句以及 SELECT 命令从表中选择数据。

如果您还没有创建数据库和表,请参考以下指南:

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

出于演示目的,我创建了一个名为 “销售量” 在一个名为的 MySQL 数据库中 “我的公司” 具有以下架构和记录的数据库。

查看 MySQL 数据库中的表记录

小心: 在 XAMPP 中,数据库和表名不区分大小写。 它将仅将大写/小写视为小写。

使用 LIKE 运算符从 MySQL 表中过滤数据

MySQL LIKE 运算符检查特定字符串是否与指定模式匹配。

换句话说,LIKE 是应用于列中的字符串值的运算符,以根据给定的表达式条件获取值。

我们在 LIKE 运算符之后指定一个表达式。 该表达式用于比较列中的字符串。 如果表达式存在于字符串中,它将被显示。

支持的表达式如下:

  • %character – 如果列以指定字符结尾,则返回列中的值。
  • character% – 如果列以指定字符开头,则返回列中的值。
  • character% character – 如果列以指定字符开头和结尾,则返回列中的值。
  • %sub-string% – 如果它在值的任何位置包含给定的子字符串,则返回该值。
  • character__ ( 2 个下划线) – 如果列以指定字符开头且字符长度为,则返回列中的值 3. 请注意,长度取决于带有字符的下划线。
  • __character ( 2 个下划线) – 如果列以指定字符结尾且字符长度为 3,则返回列中的值。

MySQL LIKE 运算符语法:

SELECT column1,column2,.,column n from table_name WHERE column_name LIKE expression

脚步

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

2. 使用 mysqli_connect() 函数建立连接。

3. 使用 WHERE 子句和 LIKE 运算符编写 SQL 查询以过滤 MySQL 数据库中的数据。

代码:

$query = "SELECT column1,…. from Sales where column_name LIKE expression";

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

代码:

mysqli_query($connection, $query);

5. 使用从“最终”变量中一一获取行 mysqli_num_rows() 功能。 然后通过迭代来获取结果 while loop 使用 mysqli_fetch_assoc() 功能。

代码:

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. 最后,我们将 close 通过使用连接 mysqli_close() 功能。

句法:

mysqli_close($connection);

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

使用 LIKE 运算符从 MySQL 数据库中选择数据的 PHP 代码

示例代码 1:

在这个 example, 我们会 选择所有列 从名称列中的值的“销售”表中,

  • 开始 带信 ‘E’,
  • 结束‘的’,
  • 包含 字母 ‘gg’.

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

小心: 如果您使用 Linux,则 文档 文件夹将在 /opt/lampp/ 目录。 如果您使用的是 Windows,则 文档 通常会在 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
$database_name = "my_company";

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

//select all columns such that name column starts with 'E'
$query = "SELECT * from sales where name like 'E%'";

#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";
}

//take a new line
echo "<br>";
echo "<br>";

//select all columns such that name column ends with 's'
$query1 = "SELECT * from sales where name like '%s'";

#get the result
$final1 = mysqli_query($connection, $query1);

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

//take a new line
echo "<br>";
echo "<br>";
//select all columns such that name column  contains 'gg'
$query2 = "SELECT * from sales where name like '%gg%'";

#get the result
$final2 = mysqli_query($connection, $query2);

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

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

打开您的网络浏览器并将其指向 https://localhost/select.php 网址。

示例 1 – 使用 WHERE 子句和 LIKE 运算符从 MySQL 数据库中选择数据

示例代码 2:

在这个 example,我们将从 Sales 表中选择所有列,同时过滤 name 列中的值 仅以“C”开头并以“s”结尾.

<?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);

//select all columns such that name column starts with 'C' and ends with 's'
$query = "SELECT * from sales where name like 'C%s'";

#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);
?>

打开您的网络浏览器并导航到 https://localhost/select.php 网址。

示例 2 - 使用 WHERE 子句和 LIKE 运算符从 MySQL 数据库中选择数据示例 2 – 使用 WHERE 子句和 LIKE 运算符从 MySQL 数据库中选择数据

示例代码 3:

在这个 example,我们将从“销售”表中选择名称列中的所有列,

  • 以 ‘C’ 开头,长度 – 6(这里我们需要在 ‘C’ 之后放置 5 个下划线),
  • 以 ‘s’ 和长度 – 4 结尾(这里我们需要在 ‘s’ 之前放置 3 个下划线)。
<?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);

// name  starts with 'C' and length - 6
$query = "SELECT * from sales where name like 'C_____'";

#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";
}

//new line
echo "<br>";
echo "<br>";

// name  ends  with 's' and length - 4
$query1 = "SELECT * from sales where name like '___s'";

#get the result
$final1 = mysqli_query($connection, $query1);

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

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

打开您的网络浏览器并将其指向 https://localhost/select.php 网址。

示例 3 - 使用 WHERE 子句和 LIKE 运算符从 MySQL 数据库中选择数据示例 3 – 使用 WHERE 子句和 LIKE 运算符从 MySQL 数据库中选择数据

结论

在本指南中,我们通过 3 个不同的示例讨论了如何通过 LIKE 运算符使用 PHP 指定表达式来从 MySQL 表中选择特定列或所有列。