当前位置:首页 > PHP教程 > PHP高级教程

php数据访问之查询关键字

本文实例为大家分享了php查询操作的实现代码,供大家参考,具体内容如下

一、一个关键字查询

主页面:

<html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>汽车查询页面</title> </head> <body> <h1>汽车查询页面</h1> <?php   include("qiche.class.php");   $db=new qiche();   //保留输入查询的内容   $cx="";   $value="";   if(!empty($_post["name"]))//判断查询内容是否为空   { $name=$_post["name"]; $cx=" where name like '%{$name}%'";//查询的字符串 $value=$name;   } ?> <br> <form action="qiche.php" method="post"> <div> 请输入查询内容:<input type="text" name="name" value="<?php echo $value; ?>"/> <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> </tr> <?php   $sql="select * from car".$cx;   $attr=$db->query($sql);   foreach ($attr as $v)   { //使输入查询的关键字变色,处理name //$rp="<mark>{$value}</mark>"; $rp="<span style='color:red'>{$value}</span>"; $arr=str_replace($value,$rp,$v[1]); echo "<tr> <td>{$v[0]}</td> <td>{$arr}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> <td>{$v[7]}</td> </tr>";   } ?> </table> </body> </html>

封装类: 

<?php class qiche { public $localhost="localhost";//服务器 public $uid="root";//用户名 public $password="";//密码 //执行查询语句sql方法: //参数的含义:$sql代表要执行的sql语句;$type代表sql语句的类型,自义0为查询,1为其他(增删改查);$db代表要查询的数据库 public function query($sql,$type="0",$db="mydb") { $dbconnect=new mysqli($this->localhost,$this->uid,$this->password,$db); !mysqli_connect_error() or die("连接失败 !"); $result=$dbconnect->query($sql); if($type==0) { return $result->fetch_all(); } else { return $result; } } }

运行结果:

二、多个关键字查询

主页面:

<html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>汽车查询页面</title> </head> <body> <h1>汽车查询页面</h1> <br> <?php include ("./dbda.class.php"); $db=new dbda(); $cx=""; $value=""; $value1=""; $tj1=" 1=1";//条件1的判断name $tj2=" 1=1";//条件2的判断brand if(!empty($_post["name"])) { $name=$_post["name"]; $tj1="name like '%{$_post['name']}%'"; $value=$name; } if(!empty($_post["brand"])) { $name1=$_post["brand"]; $tj2="brand= '{$_post['brand']}'"; $value1=$name1; } $cx=" where $tj1 and $tj2";//查询字符串 ?> <form action="chaxun1.php" method="post"> <div> 请输入名称:<input type="text" name="name" value="<?php echo $value; ?>"/> 系列:<input type="text" name="brand" value="<?php echo $value1; ?>"> <input type="submit" name="" 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".$cx; $attr=$db->query($sql); foreach ($attr as $v) { //处理name //$rp="<mark>{$value}</mark>"; $rp="<span style='color:red'>{$value}</span>"; $str=str_replace($value,$rp,$v[1]); echo "<tr> <td>{$v[0]}</td> <td>{$str}</td> <td>{$v[2]}</td> <td>{$v[7]}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> </tr>"; } ?> </table> </body> </html>

运行结果:

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!