问题陈述 :
在本文中,我们将在 Xampp 服务器中使用 LIKE运算符和 SQL 显示数据。
这里我们以学生地址数据库为例。
要求:
Xampp
介绍:
PHP代表超文本预处理器。它用作服务器端脚本语言,可用于通过 xampp 工具连接 MySQL 服务器。
MySQL 是一种用于管理数据库的查询语言。
- 喜欢运算符
SQL 中的 LIKE运算符在 WHERE 子句中用于搜索列中的指定模式。
有两个通配符可以与 LIKE运算符结合使用。他们是:
- 百分号 (%) 代表零、一个或多个字符
- 下划线 (_) 代表一个,单个字符。
句法:
SELECT column1, column2, ...,columnn
FROM table_name
WHERE columnn LIKE pattern;
描述
- letter% = 给出以给定字母开头的结果
例子:
考虑下表:
询问:
地址以 h 开头:
SELECT * from student_address WHERE saddress LIKE 'h%'
输出:
Address starts with h:
STUDENT-ID : 3 ----- NAME : ojaswi ----- ADDRESS : hyderabad
STUDENT-ID : 4 ----- NAME : rohith ----- ADDRESS : hyderabad
STUDENT-ID : 5 ----- NAME : gnanesh ----- ADDRESS : hyderabad
询问:
名称以 h 结尾:
SELECT * from student_address WHERE sname LIKE '%h';
输出:
Name ends with h:
STUDENT-ID : 4 ----- NAME : rohith ----- ADDRESS : hyderabad
STUDENT-ID : 5 ----- NAME : gnanesh ----- ADDRESS : hyderabad
询问:
地址包含“嗯”模式
SELECT * from student_address WHERE sname LIKE '%um%';
输出:
STUDENT-ID : 1 ----- NAME : sravan kumar ----- ADDRESS : kakumanu
询问:
地址以 r 开头,以 h 结尾。
SELECT * from student_address WHERE sname LIKE 'r%h';
输出:
STUDENT-ID : 4 ----- NAME : rohith ----- ADDRESS : hyderabad.
方法:
- 创建数据库(命名数据库)并创建名为 student_address 的表
- 使用PHP向表中插入数据
- 编写PHP代码来执行类似操作
- 观察结果
脚步:
- 启动 xampp 服务器。
- 创建名为database的数据库并创建一个名为student_address的表
- 编写PHP代码将记录插入其中。 (PHP)
PHP
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//insert records into table
$sql = "INSERT INTO student_address VALUES (1,'sravan kumar','kakumanu');";
$sql .= "INSERT INTO student_address VALUES (2,'bobby','kakumanu');";
$sql .= "INSERT INTO student_address VALUES (3,'ojaswi','hyderabad');";
$sql .= "INSERT INTO student_address VALUES (4,'rohith','hyderabad');";
$sql .= "INSERT INTO student_address VALUES (5,'gnanesh','hyderabad');";
if ($conn->multi_query($sql) === TRUE) {
echo "data stored successfully";
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
$conn->close();
?>
PHP
"; echo "Like operator demo: "; echo"";
echo "
";
echo "address starts with h:";
echo "
";
echo "
";
//sql query
$sql = "SELECT * from student_address WHERE saddress LIKE 'h%'";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "
";
}
echo "
";
echo "name starts with s ";
echo "
";
echo "
";
//sql query
$sql1 = "SELECT * from student_address WHERE sname LIKE 's%'";
$result1 = $conn->query($sql1);
//display data on web page
while($row = mysqli_fetch_array($result1)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "
";
}
//close the connection
$conn->close();
?>
PHP
"; echo "Like operator demo: "; echo"";
echo "
";
echo "name ends with h:";
echo "
";
echo "
";
//sql query
$sql = "SELECT * from student_address WHERE sname LIKE '%h'";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "
";
}
echo "
";
echo "address ends with u ";
echo "
";
echo "
";
//sql query
$sql1 = "SELECT * from student_address WHERE saddress LIKE '%u'";
$result1 = $conn->query($sql1);
//display data on web page
while($row = mysqli_fetch_array($result1)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "
";
}
//close the connection
$conn->close();
?>
PHP
"; echo "Like operator demo: "; echo"";
echo "
";
echo "address contains um:";
echo "
";
echo "
";
//sql query
$sql = "SELECT * from student_address WHERE sname LIKE '%um%'";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "
";
}
echo "
";
echo "name starts with r and ends with h ";
echo "
";
echo "
";
//sql query
$sql1 = "SELECT * from student_address WHERE sname LIKE 'r%h'";
$result1 = $conn->query($sql1);
//display data on web page
while($row = mysqli_fetch_array($result1)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "
";
}
//close the connection
$conn->close();
?>
打开浏览器并输入“localhost.data1. PHP ”来执行它。
输出:
data stored successfully
- 字母的 like运算符的PHP代码演示以以下开头:
形式。 PHP
PHP
"; echo "Like operator demo: "; echo"";
echo "
";
echo "address starts with h:";
echo "
";
echo "
";
//sql query
$sql = "SELECT * from student_address WHERE saddress LIKE 'h%'";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "
";
}
echo "
";
echo "name starts with s ";
echo "
";
echo "
";
//sql query
$sql1 = "SELECT * from student_address WHERE sname LIKE 's%'";
$result1 = $conn->query($sql1);
//display data on web page
while($row = mysqli_fetch_array($result1)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "
";
}
//close the connection
$conn->close();
?>
输出:
本地主机/表单。 PHP
- 一个字母的PHP代码演示以:
形式1。 PHP
PHP
"; echo "Like operator demo: "; echo"";
echo "
";
echo "name ends with h:";
echo "
";
echo "
";
//sql query
$sql = "SELECT * from student_address WHERE sname LIKE '%h'";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "
";
}
echo "
";
echo "address ends with u ";
echo "
";
echo "
";
//sql query
$sql1 = "SELECT * from student_address WHERE saddress LIKE '%u'";
$result1 = $conn->query($sql1);
//display data on web page
while($row = mysqli_fetch_array($result1)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "
";
}
//close the connection
$conn->close();
?>
输出:
- 子字符串匹配和字母开头的PHP代码演示
形式2。 PHP
PHP
"; echo "Like operator demo: "; echo"";
echo "
";
echo "address contains um:";
echo "
";
echo "
";
//sql query
$sql = "SELECT * from student_address WHERE sname LIKE '%um%'";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "
";
}
echo "
";
echo "name starts with r and ends with h ";
echo "
";
echo "
";
//sql query
$sql1 = "SELECT * from student_address WHERE sname LIKE 'r%h'";
$result1 = $conn->query($sql1);
//display data on web page
while($row = mysqli_fetch_array($result1)){
echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;
echo "
";
}
//close the connection
$conn->close();
?>
输出: