zoukankan      html  css  js  c++  java
  • 5月6 查询与多条件查询

    封装类主要是为了以后调用起来较为方便:类名叫做DBDA.class.php在www下的test0506下,以后引用是特别要注意,特别是不在同一级下的引用。

    一:只有一个条件的查询

    类:DBDA.class.php

    <?php
    class DBDA
    
    {
    	public $host = "localhost";//数据库地址
    	public $uid = "root";//数据库用户名
    	public $pwd = "";//数据库密码
    	
    	//执行SQL语句,返回相应的方法
    	//参数:$sql代表要执行的SQL语句,$type是SQL语句类型0代表查询1代表其他,$db代表要操作的数据库
    	public function Query($sql,$type=0,$db="mydb")//经常操作的数据库名称
    	{
    		//造连接对象
    		$dbconnect = new MySQLi($this->host,$this->uid,$this->pwd,$db);	
    		//判断连接是否有误
    		!mysqli_connect_error() or die("连接失败");
    		//执行SQL语句
    		$result = $dbconnect->query($sql);
    		
    		if($type==0)//查询时肯定是返回数组的
    		{
    			return $result->fetch_all();
    		}
    		else//增删改时只看看执行是否成功即可
    		{
    			return $result;	
    		}
    	}	
    }
    

      页面显示的内容

    <body>
    <h1>汽车查询页面</h1>
    <?php
    
    	include("DBDA.class.php");
       	$db = new DBDA();
    	
    	$cx = "";
    	$value = "";
    	if(!empty($_POST["name"]))//有值不为空
    	{
    		$name = $_POST["name"];//取输入的name的名称
    		$cx = " where name like '%{$name }%'";//查询字符串
    		$value = $name;
    	}
    ?>
    
    <form action="ZHL.php" method="post">
    <div>请输入名称:<input type="text" name="name" value="<?php echo $value ?>" /> 
    			   <input type="submit" value="提交" />
    </div>
    </form>
    <br /><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);//Query是方法
    	
    	foreach($attr as $v)
    	{
    		//处理字符串
    		$rp = "<span style='background: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>
       
    </body>
    </html>
    

      页面显示的效果:

    查询结果:

    二:多条件查询,只写其中一个或两个全写出来都可以查到

    页面显示的内容:

    <body>
    <h1>汽车查询页面</h1><br />
    <?php
    	
    	include("./DDDA.class.php");
    	$db = new DDDA();
    	
    	$cx="";
    	$value ="";
    	$value1 ="";
    	
    	
    	$tj1 = " 1=1";//条件1的判断,永远是true默认恒成立可以随便写
    	$tj2 = " 1=1";//条件2的判断
    	
    	if(!empty($_POST["name"]))
    	{
    		$tj1 = " Name like '%{$_POST['name']}%'";
    		$value = $_POST["name"];
    	}
    	if(!empty($_POST["brand"]))
    	{
    		$tj2 = "Brand = '{$_POST['brand']}'";	
    		$value1 = $_POST["brand"];
    	}
    	
    	$cx = " where {$tj1} and {$tj2} ";
    	
    ?>
    
    <form action="xw.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" value="查询" />
    </div>
    
    </form>
    <br />
    <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>";
    		$rp1 = "<mark>{$value1}</mark>";
    		//$rp ="<span></span>";
    		$str = str_replace($value,$rp,$v[1]);
    		$str1 = str_replace($value1,$rp1,$v[2]);
    		echo "<tr>
    				<td>{$v[0]}</td>
    				<td>{$str}</td>
    				<td>{$str1}</td>
    				<td>{$v[7]}</td>
    				<td>{$v[4]}</td>
    				<td>{$v[5]}</td>			
    		     </tr>";	
    	}
    ?>
    </table>
    </body>
    </html>
    

      

  • 相关阅读:
    二进制位运算
    Leetcode 373. Find K Pairs with Smallest Sums
    priority_queue的用法
    Leetcode 110. Balanced Binary Tree
    Leetcode 104. Maximum Depth of Binary Tree
    Leetcode 111. Minimum Depth of Binary Tree
    Leetcode 64. Minimum Path Sum
    Leetcode 63. Unique Paths II
    经典的递归练习
    案例:java中的基本排序
  • 原文地址:https://www.cnblogs.com/Duriyya/p/5470740.html
Copyright © 2011-2022 走看看