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>