关键字查询和多条件查询

时间:2021-08-14 05:37:36

0616DBDA.class.php 代码

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/16
 * Time: 11:23
 */
class DBDA
{
    public $host="localhost";//服务器地址
    public $uid="root" ;//用户名
    public $pwd="";//密码

    public $dbconnect;//链接对象

    function Query($sql,$type,$dbname="aaas")//sql语句;type语句类型,数字来表示;数据库的默认值
    {
        $this->dbconnect = new mysqli($this->host,$this->uid,$this->pwd,$dbname);
        //判断是否出错
        if(!mysqli_connect_error())
        {
            //如果连接成功,执行SQL语句
            $result = $this->dbconnect->query($sql);

            //根据语句类型判断
            if($type==1)
            {
                //如果是查询语句,返回二维数组
                return $result->fetch_all();
            }
            else
            {
                //如果是其他语句,返回true或false
                return $result;
            }
        }
        else
        {
            return "连接失败!";
        }
    }

}

 

【1.关键字查询,且关键字显示紫色】

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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>


<form action="0616aacx.php" method="post">
<div>输入内容:<input type="text" name="name" >
<input type="submit" value="搜索"></div>

    </form>
<table width="100%" border="1" cellspacing="0" cellpadding="0">

    <tr>
        <td>代号</td>
        <td>名称</td>
        <td>车型</td>
        <td>发布时间</td>
        <td>耗油</td>
    </tr>
    <?php
    include("0616DBDA.class.php");
    $db=new DBDA();

     $tj="";
    $name="";
   if(!empty($_POST))
     {
         $name=$_POST["name"];
         $tj="where name like '%{$name}%'";
     }

    $sql="select * from car ".$tj;

    echo $sql;

    $attr=$db->query($sql,1);

    foreach($attr as $v)
    {

        $re= "<span style='color:#ff33f1'>{$name}</span>";
        $str=str_replace($name,$re,$v[1]);

        echo "
        <tr>
        <td>{$v[0]}</td>
        <td>{$str}</td>
        <td>{$v[2]}</td>
        <td>{$v[3]}</td>
        <td>{$v[4]}</td>
</tr>
        ";
    }

    ?>

</table>
</body>
</html>

关键字查询和多条件查询

【2.多条件查询】

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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>
<form action="0616aaa.php" method="post">
<div>
    输入汽车名称
    <input type="text" name="name">

    输入系列代号
    <input type="text" name="brand">
    <input type="submit" value="查询">
    </div>
</form>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
    <td>code</td>
    <td>name</td>
    <td>brand</td>
    <td>time</td>
    <td>oil</td>
    <td>powers</td>
<tr>
        <?php

        //一般来说,用户不输入任何东西,默认查询所有的

        include("0616DBDA.class.php");
        $db=new DBDA();

        $tj1=" 1=1";
        $tj2=" 1=1";
        $name="";

        if(!empty($_POST))
        {
            if ($_POST["name"]!="")
            {
                $name = $_POST['name'];
                $tj1 = " name like '%{$name}%'";
            }
            if ($_POST["brand"]!="")//如果不为空,进来
            {

                $tj2=" brand= '{$_POST['brand']}'";
            }
        }


        $cxtj=" where".$tj1." and ".$tj2;
        $sql="select * from car ".$cxtj;//最后加空格,条件拼接起来
        echo $sql;
        $attr=$db->Query($sql,1);
        foreach($attr as $v)
        {
            //处理输出字符串,改变颜色
            $v[1];$name;
            $re="<span style='color:chartreuse'>{$name}</span>";
            $str=str_replace($name,$re,$v[2]);

            echo "
            <tr>
            <td>{$v[0]}</td>
    <td>{$v[1]}</td>
    <td>{$str}</td>
    <td>{$v[3]}</td>
    <td>{$v[4]}</td>
    <td>{$v[5]}</td>
</tr>
            ";
        }
        ?>


</table>




</body>
</html>