zoukankan      html  css  js  c++  java
  • MYSQLi数据访问查询数据

    单条件查询

    <body>
    	<div align="center" style="90%;">
        <h1>数据查询</h1>
        <form method="post" action="test-1.php">
        <div>请输入汽车品牌:<input type="text" name="name"/></div>
        <br />
        <div><input type="submit" value="查询" name="chaxun"/></div>
        <br />
        </form>
        <div>
        	<table width="90%" border="1" align="center" style="text-align:center;">
            	<tr>
                	<td>汽车代号</td>
                    <td>汽车名字</td>
                    <td>系列代号</td>
                    <td>汽车油耗</td>
                    <td>汽车价格</td>
                </tr>
    	<?php 
    		$tj = " 1=1 ";//恒成立条件
    		$name = "";
    		if(!empty($_POST)){
    			$name = $_POST["name"];
    			$tj = " name like '%{$name}%' " ;
    		}
        	$db = new MySQLi("localhost","root","123456","crud");
    		$sql = "select * from car where {$tj}";
    		$result = $db->query($sql);
    		if($result){
    			$arr = $result->fetch_all();
    			foreach($arr as $v){
    				$v[1];$name;
    				$carname = str_replace($name,"<font color='#FF0000'>{$name}</font>",$v[1]);//字符串替换
    				echo"<tr>
    			<td>{$v[0]}</td>
    			<td>{$carname}</td>
    			<td>{$v[2]}</td>
    			<td>{$v[4]}</td>
    			<td>{$v[7]}</td>	
    				</tr>";
    			}
    		}
        ?>
            </table>
        </div>
        </div>
    </body>
    

     

     多条件查询

    <body>
    	<div align="center" style="90%;">
        <h1>数据查询</h1>
        <form method="post" action="test-1.php">
        <div>请输入汽车品牌:<input type="text" name="name"/>  
        请输入系列代号:<input type="text" name="brand"/></div> <!--两个条件-->
        <br />
        <div><input type="submit" value="查询" name="chaxun"/></div>
        <br />
        </form>
        <div>
        	<table width="90%" border="1" align="center" style="text-align:center;">
            	<tr>
                	<td>汽车代号</td>
                    <td>汽车名字</td>
                    <td>系列代号</td>
                    <td>汽车油耗</td>
                    <td>汽车价格</td>
                </tr>
    	<?php 
    		$tj = " 1=1 ";//恒成立条件
    		$tj1 = " 1=1 ";//恒成立条件
    		$name = "";
    		$brand = "";
    		if(!empty($_POST["name"])){
    			$name = $_POST["name"];
    			$tj = " name like '%{$name}%' " ;
    		}
    		if(!empty($_POST["brand"])){
    			$brand = $_POST["brand"];
    			$tj1 = " brand = '{$brand}' " ;
    		}
        	$db = new MySQLi("localhost","root","123456","crud");
    		$sql = "select * from car where {$tj} and {$tj1}";
    		$result = $db->query($sql);
    		if($result){
    			$arr = $result->fetch_all();
    			if(!empty($arr)){
    				echo "数据不存在!";
    			}else{
    				foreach($arr as $v){
    				$v[1];$name;
    				$v[2];$brand;
    				$carname = str_replace($name,"<font color='#FF0000'>{$name}</font>",$v[1]);//字符串替换
    				$carbrand = str_replace($brand,"<font color='#FF0000'>{$brand}</font>",$v[2]);
    				echo"<tr>
    			<td>{$v[0]}</td>
    			<td>{$carname}</td>
    			<td>{$carbrand}</td>
    			<td>{$v[4]}</td>
    			<td>{$v[7]}</td>	
    				</tr>";
    				}	
    			}
    		}
        ?>
            </table>
        </div>
        </div>
    </body>
    

     

  • 相关阅读:
    7月30日 举办专注于微服务的.NET Conf Focus
    我和ABP vNext 的故事
    Windows环境搞好的Dockerfile文件 在Linux上报错了
    [LeetCode] 955. Delete Columns to Make Sorted II 删除列使其有序之二
    [LeetCode] 954. Array of Doubled Pairs 两倍数对儿数组
    上周热点回顾(8.3-8.9)团队
    发布新版首页“外婆新家”升级版:全新的UI,熟悉的味道团队
    上周热点回顾(7.27-8.2)团队
    终于换新颜:新版网站首页发布上线团队
    上周热点回顾(7.20-7.26)团队
  • 原文地址:https://www.cnblogs.com/navyouth/p/8301572.html
Copyright © 2011-2022 走看看