MYSQLi数据访问查询数据

Posted navyyouth

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQLi数据访问查询数据相关的知识,希望对你有一定的参考价值。

单条件查询

<body>
	<div align="center" style="width: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="width: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>

 

 

以上是关于MYSQLi数据访问查询数据的主要内容,如果未能解决你的问题,请参考以下文章

无法访问 PHPMyAdmin 数据库的查询

mysqli查询成功但给出错误号0 [关闭]

函数内部的mysqli / mysql查询不起作用[重复]

从 mysql 更新到 mysqli 后数据库查询中断

PHP - 在 mysqli 查询中使用数组

PHP数据访问(面向对象方式:mysqli类)