首页 > 代码库 > php->msql 多条件查询
php->msql 多条件查询
d多条件查询
body> <br /> <form action="duotiaojian.php" method="post"> <div>姓名: <input type="text" name="xm" /> 民族代号: <input type="text" name="mz" /> <input type="submit" value=http://www.mamicode.com/"查询" /> </div> </form> <br /> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>生日</td> <td>操作</td> </tr> <?php //先判断有没有提交值 $xxm = ""; $tj = " 1=1 "; $tj2 = " 1=1 "; if(!empty($_POST["xm"]) && $_POST["xm"]!="") { $xxm = $_POST["xm"]; $tj = " name like ‘%{$xxm}%‘ "; } if(!empty($_POST["mz"]) && $_POST["mz"]!="") { $mz = $_POST["mz"]; $tj2 = " nation=‘{$mz}‘ "; } //造连接对象 $db = new MySQLi("localhost","root","123","mydb"); //写SQL语句 $sql = "select * from info where ".$tj." and ".$tj2; echo $sql; //执行SQL语句 $result = $db->query($sql); //读数据 $attr = $result->fetch_all(); foreach($attr as $v) { echo "<tr>"; $sex = $v[2]?"男":"女"; //根据名族代号查询名族名称 $name = NationName($v[3]); //替换关键字 $newname = str_replace($xxm,"<mark>{$xxm}</mark>",$v[1]); echo "<td>{$v[0]}</td><td>{$newname}</td><td>{$sex}</td><td>{$name}</td><td>{$v[4]}</td><td><a href=http://www.mamicode.com/‘shanchu.php?c={$v[0]}‘ onclick=\"return confirm(‘确定删除么?‘)\">删除</a><a href=http://www.mamicode.com/‘xiugai.php?c={$v[0]}‘>修改"; /*foreach($v as $v1) { echo "<td>{$v1}</td>"; }*/ echo "</tr>"; } //给一个民族代号,返回民族名称 function NationName($code) { //造连接对象 $db = new MySQLi("localhost","root","123","mydb"); //写SQL语句 $sql = "select name from nation where code=‘{$code}‘"; //执行SQL语句 $result = $db->query($sql); $attr = $result->fetch_row(); return $attr[0]; } ?> </table> <a href=http://www.mamicode.com/"add.php"><input type="button" value=http://www.mamicode.com/"添加数据" /></a> </body> </html>
php->msql 多条件查询
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。