首页 > 代码库 > php 多条件查询 例子

php 多条件查询 例子

<hl>表单的多条件查询</h1>

<form action="这个表.php" method="post">

<div>
请输入查询的名字:<input type="text" name="name" />
请输入查询址性别:<input type="text" name="sex" />
<input type="submit" value="http://www.mamicode.com/查询" />
</div>

</form>

<br />

<table width="100%" border="1" cellpadding="0" cellspacing="0">

<tr>
<td>ID</td>
<td>名字</td>
<td>性别</td>
<td>生日</td>
<td>地址</td>
</tr>

<?php

$db = new MySQLi("localhost","root","root","数据库名");

$tj1 = " 1 = 1 ";
$tj2 = " 1 = 1 ";
if(!empty($_POST["name"]))
{
$name = $_POST[‘name‘];
$tj1 = " name like ‘%{$name}%‘ ";
}

if(!empty($_POST["sex"]))
{
$sex = $_POST["sex"];
$tj2 = " sex = ‘{$sex}‘";
}

$sql = "select * from 表名 where {$tj1} and {$tj2}";
echo $sql;

$result = $db -> query($sql);

while($arr = $result -> fetch_row())
{

echo "<tr>
<td>{$arr[0]}</td>
<td>{$arr[1]}</td>
<td>{$arr[2]}</td>
<td>{$arr[3]}</td>
<td>{$arr[5]}</td>
</tr>";
}

?>

</table>

php 多条件查询 例子