首页 > 代码库 > php多关键字查询
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="http://www.mamicode.com/查询" /> </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="http://www.mamicode.com/<?php echo $name ?>" /> 请输入系列代号: <input type="text" name="brand" /> <input type="submit" value="http://www.mamicode.com/查询" /> </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多关键字查询
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。