如何通过PHP和MySQL实现员工信息的模糊查询功能示例?
- 内容介绍
- 文章标签
- 相关推荐
本文共计963个文字,预计阅读时间需要4分钟。
本例展示了如何使用PHP和MySQL实现模糊查询员工信息功能。以下是一段代码示例,包括代码和注意事项:
代码:
php
$conn=new mysqli($servername, $username, $password, $dbname);
// 检查连接if ($conn->connect_error) { die(连接失败: . $conn->connect_error);}
// 获取用户输入$keyword=$_GET['keyword'];
// 模糊查询$sql=SELECT id, name, email FROM employees WHERE name LIKE '%$keyword%';
$result=$conn->query($sql);
if ($result->num_rows > 0) { // 输出数据 while($row=$result->fetch_assoc()) { echo ID: . $row[id]. - Name: . $row[name]. - Email: . $row[email]. ; }} else { echo 0 结果;}
$conn->close();?>
注意事项:
1. 使用Notepad++编辑代码时,选择以下编码格式: - 编码字符集:[UTF-8] - 编码方式:[gb2312]
2. 确保数据库连接信息(服务器名、用户名、密码、数据库名)正确。
3. 根据实际需求修改SQL查询语句,例如更改表名或字段名。
4. 考虑到安全性,建议对用户输入进行过滤和验证,以防止SQL注入攻击。
本文实例讲述了PHP+MySQL实现模糊查询员工信息功能。分享给大家供大家参考,具体如下:
一、代码
注意两点:
1、用Notepad+编辑时,格式选择:->->
2、
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
conn.php
<?php $connID=mysql_connect("localhost","root","root"); mysql_select_db("db_database13", $connID); mysql_query("set names gbk"); ?>
index.php
<?php session_start(); include("conn/conn.php");?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title> 查询企业员工的详细信息</title> <style type="text/css"> <!-- .STYLE4 {font-size: 13px} .STYLE5 {font-family: "华文琥珀"; font-size: 20px;} .STYLE6 {color: #990000} --> </style> </head> <body> <table width="450" height="28" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td align="center" class="STYLE5 STYLE6">查询企业员工的详细信息</td> </tr> </table> <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>"> <table width="450" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#990000"> <tr> <td width="176" align="center" bgcolor="#FFFFFF"><span class="STYLE1 STYLE4">按照员工的编号查询</span></td> <td width="182" bgcolor="#FFFFFF"><input name="number" type="text" id="number" /></td> <td width="84" height="25" bgcolor="#FFFFFF"><input type="hidden" name="flag" value="1" /><input name="submit" type="submit" value="提交" /></td> </tr> </table> </form> <table width="450" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#990000"> <tr> <td width="67" align="center" bgcolor="#FFFFFF" class="STYLE4">编号</td> <td width="72" align="center" bgcolor="#FFFFFF" class="STYLE4">姓名</td> <td width="119" align="center" bgcolor="#FFFFFF" class="STYLE4">电话</td> <td width="182" height="25" align="center" bgcolor="#FFFFFF" class="STYLE4">地址</td> </tr> <?php if(isset($_POST["flag"])) { $query=mysql_query("select * from tb_employee where number like '%".$_POST["number"]."%'"); if($query) { while($myrow=mysql_fetch_array($query)) { ?> <tr> <td align="center" bgcolor="#FFFFFF" class="STYLE4"><span class="STYLE2"><?php echo $myrow[number];?></span></td> <td align="center" bgcolor="#FFFFFF" class="STYLE4"><span class="STYLE2"><?php echo $myrow[name];?></span></td> <td height="23" align="center" bgcolor="#FFFFFF" class="STYLE4"><span class="STYLE2"><?php echo $myrow[tel];?></span></td> <td height="23" align="center" bgcolor="#FFFFFF" class="STYLE4"><span class="STYLE2"><?php echo $myrow[address];?></span></td> </tr> <?php } } } ?> </table> </body> </html>
二、运行结果
浏览器选择的编码是gbk
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
本文共计963个文字,预计阅读时间需要4分钟。
本例展示了如何使用PHP和MySQL实现模糊查询员工信息功能。以下是一段代码示例,包括代码和注意事项:
代码:
php
$conn=new mysqli($servername, $username, $password, $dbname);
// 检查连接if ($conn->connect_error) { die(连接失败: . $conn->connect_error);}
// 获取用户输入$keyword=$_GET['keyword'];
// 模糊查询$sql=SELECT id, name, email FROM employees WHERE name LIKE '%$keyword%';
$result=$conn->query($sql);
if ($result->num_rows > 0) { // 输出数据 while($row=$result->fetch_assoc()) { echo ID: . $row[id]. - Name: . $row[name]. - Email: . $row[email]. ; }} else { echo 0 结果;}
$conn->close();?>
注意事项:
1. 使用Notepad++编辑代码时,选择以下编码格式: - 编码字符集:[UTF-8] - 编码方式:[gb2312]
2. 确保数据库连接信息(服务器名、用户名、密码、数据库名)正确。
3. 根据实际需求修改SQL查询语句,例如更改表名或字段名。
4. 考虑到安全性,建议对用户输入进行过滤和验证,以防止SQL注入攻击。
本文实例讲述了PHP+MySQL实现模糊查询员工信息功能。分享给大家供大家参考,具体如下:
一、代码
注意两点:
1、用Notepad+编辑时,格式选择:->->
2、
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
conn.php
<?php $connID=mysql_connect("localhost","root","root"); mysql_select_db("db_database13", $connID); mysql_query("set names gbk"); ?>
index.php
<?php session_start(); include("conn/conn.php");?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title> 查询企业员工的详细信息</title> <style type="text/css"> <!-- .STYLE4 {font-size: 13px} .STYLE5 {font-family: "华文琥珀"; font-size: 20px;} .STYLE6 {color: #990000} --> </style> </head> <body> <table width="450" height="28" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td align="center" class="STYLE5 STYLE6">查询企业员工的详细信息</td> </tr> </table> <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>"> <table width="450" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#990000"> <tr> <td width="176" align="center" bgcolor="#FFFFFF"><span class="STYLE1 STYLE4">按照员工的编号查询</span></td> <td width="182" bgcolor="#FFFFFF"><input name="number" type="text" id="number" /></td> <td width="84" height="25" bgcolor="#FFFFFF"><input type="hidden" name="flag" value="1" /><input name="submit" type="submit" value="提交" /></td> </tr> </table> </form> <table width="450" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#990000"> <tr> <td width="67" align="center" bgcolor="#FFFFFF" class="STYLE4">编号</td> <td width="72" align="center" bgcolor="#FFFFFF" class="STYLE4">姓名</td> <td width="119" align="center" bgcolor="#FFFFFF" class="STYLE4">电话</td> <td width="182" height="25" align="center" bgcolor="#FFFFFF" class="STYLE4">地址</td> </tr> <?php if(isset($_POST["flag"])) { $query=mysql_query("select * from tb_employee where number like '%".$_POST["number"]."%'"); if($query) { while($myrow=mysql_fetch_array($query)) { ?> <tr> <td align="center" bgcolor="#FFFFFF" class="STYLE4"><span class="STYLE2"><?php echo $myrow[number];?></span></td> <td align="center" bgcolor="#FFFFFF" class="STYLE4"><span class="STYLE2"><?php echo $myrow[name];?></span></td> <td height="23" align="center" bgcolor="#FFFFFF" class="STYLE4"><span class="STYLE2"><?php echo $myrow[tel];?></span></td> <td height="23" align="center" bgcolor="#FFFFFF" class="STYLE4"><span class="STYLE2"><?php echo $myrow[address];?></span></td> </tr> <?php } } } ?> </table> </body> </html>
二、运行结果
浏览器选择的编码是gbk
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。

