ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP实现搜索功能

2021-06-10 20:02:29  阅读:175  来源: 互联网

标签:功能 页面 echo 搜索 mysql mysqli PHP conn row


PHP实现搜索功能

写在前面

‘’ php5与php7使用MySQL时有mysql和mysqli的区别,参数位置的变化;在不在同一页面主要取决于结果页面是否有搜索页面的代码

PHP7

搜索页面与结果在同一页面

前端

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>收录系统</title>
    <meta charset="utf-8">
  </head>
  <body>
	  <h1>搜索</h1><br/>
	<form action="search.php" method="post">
		收录内容:<input type="text" name="keywords" placeholder="搜索不到?可能还没被收录哦~">
		<input type="submit" values="搜索">
	</form>
  </body>
</html>

数据库

config.php

<?php
	$conn=mysqli_connect("IP","username","password");
	if(!$conn){
		echo "连接数据库失败!";
	}else{
mysqli_select_db($conn,"database");

		mysqli_query($conn,"set names utf8");
}

结果

search.php

<?php
	require("config.php");
	$keywords=$_POST['keywords'];
	$sql="select * from ceshis where name like '%".$keywords."%'";
	$result=mysqli_query($conn,$sql);
	if(!$result){
		die('无法读取数据,请联系管理员修复:'.mysqli_error($conn));
	}
	echo "<h1>搜索</h1><br/>
	<form action='search.php' method='post'>
		收录内容:<input type='text' name='keywords' placeholder='搜索不到?可能还没被收录哦~'>
		<input type='submit' values='搜索'>
	</form>";
	echo "<h2>Mysql where</h2>";
	echo "<table border='1'><tr><td>name</td><td>mail</td></tr>";
	while($row=mysqli_fetch_array($result)){
		echo "<tr>";
		echo "<td>{$row['name']}</td>";
		echo "<td>{$row['mail']}</td>";
		echo "</tr>";
	}
	echo "</table>";
	mysqli_close($conn);

?>

搜索页面与结果不在同一页面

前端

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>收录系统</title>
    <meta charset="utf-8">
  </head>
  <body>
	  <h1>搜索</h1><br/>
	<form action="search.php" method="post">
		收录内容:<input type="text" name="keywords" placeholder="搜索不到?可能还没被收录哦~">
		<input type="submit" values="搜索">
	</form>
  </body>
</html>

数据库

config.php

<?php
	$conn=mysqli_connect("IP","username","password");
	if(!$conn){
		echo "连接数据库失败!";
	}else{
mysqli_select_db($conn,"database");

		mysqli_query($conn,"set names utf8");
}

结果

search.php

<?php
	require("config.php");
	$keywords=$_POST['keywords'];
	$sql="select * from ceshis where name like '%".$keywords."%'";
	$result=mysqli_query($conn,$sql);
	if(!$result){
		die('无法读取数据,请联系管理员修复:'.mysqli_error($conn));
	}
	echo "<h2>Mysql where</h2>";
	echo "<table border='1'><tr><td>name</td><td>mail</td></tr>";
	while($row=mysqli_fetch_array($result)){
		echo "<tr>";
		echo "<td>{$row['name']}</td>";
		echo "<td>{$row['mail']}</td>";
		echo "</tr>";
	}
	echo "</table>";
	mysqli_close($conn);

?>

PHP5

搜索页面与结果在同一页面

前端

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>收录系统</title>
    <meta charset="utf-8">
  </head>
  <body>
	  <h1>搜索</h1><br/>
	<form action="search.php" method="post">
		收录内容:<input type="text" name="keywords" placeholder="搜索不到?可能还没被收录哦~">
		<input type="submit" values="搜索">
	</form>
  </body>
</html>

数据库

config.php

<?php
	$conn=mysqli_connect("IP","username","password");
	if(!$conn){
		echo "连接数据库失败!";
	}else{
mysql_select_db("database",$conn);

		mysql_query("set names utf8",$conn);
}

结果

search.php

<?php
	require("config.php");
	$keywords=$_POST['keywords'];
	$sql="select * from ceshis where name like '%".$keywords."%'";
	$result=mysql_query($sql,$conn);
	if(!$result){
		die('无法读取数据,请联系管理员修复:'.mysqli_error($conn));
	}
	echo "<h1>搜索</h1><br/>
	<form action='search.php' method='post'>
		收录内容:<input type='text' name='keywords' placeholder='搜索不到?可能还没被收录哦~'>
		<input type='submit' values='搜索'>
	</form>";
	echo "<h2>Mysql where</h2>";
	echo "<table border='1'><tr><td>name</td><td>mail</td></tr>";
	while($row=mysql_fetch_array($result)){
		echo "<tr>";
		echo "<td>{$row['name']}</td>";
		echo "<td>{$row['mail']}</td>";
		echo "</tr>";
	}
	echo "</table>";
	mysql_close($conn);

?>

搜索页面与结果不在同一页面

前端

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>收录系统</title>
    <meta charset="utf-8">
  </head>
  <body>
	  <h1>搜索</h1><br/>
	<form action="search.php" method="post">
		收录内容:<input type="text" name="keywords" placeholder="搜索不到?可能还没被收录哦~">
		<input type="submit" values="搜索">
	</form>
  </body>
</html>

数据库

config.php

<?php
	$conn=mysqli_connect("IP","username","password");
	if(!$conn){
		echo "连接数据库失败!";
	}else{
mysql_select_db("database",$conn);

		mysql_query("set names utf8",$conn);
}

结果

search.php

<?php
	require("config.php");
	$keywords=$_POST['keywords'];
	$sql="select * from ceshis where name like '%".$keywords."%'";
	$result=mysql_query($sql,$conn);
	if(!$result){
		die('无法读取数据,请联系管理员修复:'.mysqli_error($conn));
	}
	echo "<h2>Mysql where</h2>";
	echo "<table border='1'><tr><td>name</td><td>mail</td></tr>";
	while($row=mysql_fetch_array($result)){
		echo "<tr>";
		echo "<td>{$row['name']}</td>";
		echo "<td>{$row['mail']}</td>";
		echo "</tr>";
	}
	echo "</table>";
	mysql_close($conn);

?>

标签:功能,页面,echo,搜索,mysql,mysqli,PHP,conn,row
来源: https://blog.csdn.net/m0_37786634/article/details/117789947

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有