php多关键字查询
Posted 发瑞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php多关键字查询相关的知识,希望对你有一定的参考价值。
php单一关键字查询
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 tdansitional//EN" "http://www.w3.org/td/xhtml1/DTD/xhtml1-tdansitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <h1>查询页面</h1> <?php $db = new mysqli("localhost","root","","12345"); $tj = " 1=1 "; //条件默认恒成立 $name = ""; if(!empty($_POST["name"])) { $name = $_POST["name"]; $tj = " name like \'%{$name}%\' "; } ?> <form action="test.php" method="post"> <div>请输入名称: <input type="text" name="name" /> <input type="submit" value="查询" /> </div> </form> <br> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>名称</td> <td>系列</td> <td>时间</td> <td>油耗</td> <td>功率</td> </tr> <?php $sql = "select * from car where {$tj}"; echo $sql; $result = $db->query($sql); $attr = $result->fetch_all(); foreach($attr as $v) { $str = str_replace($name,"<mark>{$name}</mark>",$v[1]); echo "<tr> <td>{$v[0]}</td> <td>{$str}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> </tr>"; } ?> </table> </body> </html>
结果:
关键字查找“325”:
多条件查询
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 tdansitional//EN" "http://www.w3.org/td/xhtml1/DTD/xhtml1-tdansitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <h1>查询页面</h1> <?php $db = new MySQLi("localhost","root","","12345"); $tj1 = " 1=1 "; $tj2 = " 1=1 "; $name = ""; if(!empty($_POST["name"])) { $name = $_POST["name"]; $tj1 = " name like \'%{$name}%\' "; } if(!empty($_POST["brand"])) { $brand = $_POST["brand"]; $tj2 = " brand = \'{$brand}\' "; } ?> <form action="main.php" method="post"> <div>请输入名称: <input type="text" name="name" value="<?php echo $name ?>" /> 请输入系列代号: <input type="text" name="brand" /> <input type="submit" value="查询" /> </div> </form> <br> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>名称</td> <td>系列</td> <td>时间</td> <td>油耗</td> <td>功率</td> </tr> <?php $sql = "select * from car where {$tj1} and {$tj2}"; echo $sql; $result = $db->query($sql); $attr = $result->fetch_all(); if(count($attr)>0) { foreach($attr as $v) { $str = str_replace($name,"<mark>{$name}</mark>",$v[1]); echo "<tr> <td>{$v[0]}</td> <td>{$str}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> </tr>"; } } else { echo "<script>alert(\'没有查到数据\');</script>"; } ?> </table> </body> </html>
结果:
查询关键字“奥迪”,代号为“b002”的结果:
以上是关于php多关键字查询的主要内容,如果未能解决你的问题,请参考以下文章