码迷,mamicode.com
首页 > Web开发 > 详细

PHP,单项查询及多项查询

时间:2016-05-10 12:47:05      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

先封装对象
class
DBDA { public $host = "localhost"; //数据库地址 public $uid = "root"; //数据库用户名 public $pwd = "123"; //数据库密码 //执行SQL语句,返回相应的结果的方法 //参数:$sql代表要执行的SQL语句,$type是SQL语句类型0代表查询1代表其他,$db代表要操作的数据库 public function Query($sql,$type=0,$db="mydb") { //1.造连接对象 $dbconnect = new MySQLi($this->host,$this->uid,$this->pwd,$db); //2.判断连接是否出错 !mysqli_connect_error() or die("连接失败!"); //3.执行SQL语句 $result = $dbconnect->query($sql); if($type==0) { return $result->fetch_all(); } else { return $result; } } }
单项查询页面
<h1>汽车查询页面</h1>
<br />
<?php
include("./DBDA.class.php"); $db = new DBDA(); $cx=""; $value=""; if(!empty($_POST["name"])) { $name = $_POST["name"]; $cx = " where Name like ‘%{$name}%‘";//查询字符串 $value = $name; } ?> <form action="test.php" method="post"> <div> 请输入名称:<input type="text" name="name" value="<?php echo $value; ?>" /> &nbsp; <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 = "<span style=‘color:red‘>{$value}</span>"; $str = str_replace($value,$rp,$v[1]); echo "<tr> <td>{$v[0]}</td> <td>{$str}</td> <td>{$v[7]}</td> <td>{$v[4]}</td> <td>{$v[5]}</td> </tr>"; } ?> </table>
多项查询页面
<h1>汽车查询页面</h1>
<br />
<?php
    include("./DBDA.class.php");
    $db = new DBDA();
    
      $cx="";
    $value="";
    
    $tj1 = " 1=1"; //条件1的判断
    $tj2 = " 1=1"; //条件2的判断
    
    if(!empty($_POST["name"]))
    {
        $tj1 = " Name like ‘%{$_POST[‘name‘]}%‘";
    }
    if(!empty($_POST["brand"]))
    {
        $tj2 = " Brand = ‘{$_POST[‘brand‘]}‘";
    }
    
    $cx = " where {$tj1} and {$tj2} ";
?>
<form action="test.php" method="post">
<div>
    请输入名称:<input type="text" name="name" value="<?php echo $value; ?>" /> &nbsp;
    系列:<input type="text" name="brand" />&nbsp;
    <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".$cx;
        $attr = $db->Query($sql);
        echo $sql;
        
        foreach($attr as $v)
        {
            //处理Name
            $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>

 

PHP,单项查询及多项查询

标签:

原文地址:http://www.cnblogs.com/zhaimiaoer/p/5477150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!