<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <h1>查询数据</h1> <form action="biaoge.php" method="post"> <div>请输入汽车名称:<input type="text" name="name" /> <input type="submit" value="查询" /> </div> <br /> </form> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <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","123","ceshi"); $sql = "select * from car where {$tj}"; echo $sql; $relsut=$db->query($sql); if($relsut){ $arr = $relsut->fetch_all(); foreach($arr as $v){ $v[1];$name; $carname = str_replace($name,"<mark>{$name}</mark>",$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> </body> </html>
多条件查询
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <h1>查询数据</h1> <form action="duogechaxun.php" method="post"> <div>请输入汽车名称:<input type="text" name="name" /> 请输入系列代号:<input type="text" name="brand" /> <input type="submit" value="查询" /></div> <br /> </form> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>名称</td> <td>系列</td> <td>油耗</td> <td>价格</td> </tr> <?php $tj = " 1=1 ";//做了一个恒成立的条件 $tj2 = " 1=1 "; $name = ""; if(!empty($_POST["name"])){ $name = $_POST["name"]; $tj = " name like '%{$name}%' "; } if(!empty($_POST["brand"])){ $brand = $_POST["brand"]; $tj2 = " brand = '{$brand}' "; } $db=new MySQLi("localhost","root","123","ceshi"); $sql = "select * from car where {$tj} and {$tj2}"; echo $sql; $relsut=$db->query($sql); if($relsut){ $arr = $relsut->fetch_all(); foreach($arr as $v){ echo "<tr> <td>{$v[0]}</td> <td>{$v[1]}</td> <td>{$v[2]}</td> <td>{$v[4]}</td> <td>{$v[7]}</td> </tr>"; } } ?> </table> </body> </html>