作业:查询INFO表所有数据,显示在页面上(表格)
性别要显示男女 民族 显示民族名称
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>无标题文档</title> 6 </head> 7 8 <body> 9 <h1>主页面</h1> 10 <table width="100%" border="1" cellpadding="0" cellspacing="0"> 11 <tr> 12 <td>代号</td> 13 <td>姓名</td> 14 <td>性别</td> 15 <td>民族</td> 16 <td>生日</td> 17 </tr> 18 <?php 19 //造对象 20 $db = new mysqli("localhost","root","","zuoye"); 21 //判断连接是否错误 22 !mysqli_connect_error() or die("连接失败!"); 23 //写SQL语句 24 $sql = "select * from info"; 25 //执行SQL语句 26 $result = $db->query($sql); 27 //读取数据 28 $attr = $result->fetch_all(); 29 //遍历数组 30 foreach($attr as $v) 31 { 32 $sex = $v[2]?"男":"女"; 33 $sql = "select name from nation where code = '{$v[3]}'"; 34 $r = $db->query($sql); 35 $a = $r->fetch_row(); 36 echo " 37 <tr> 38 <td>{$v[0]}</td> 39 <td>{$v[1]}</td> 40 <td>{$sex}</td> 41 <td>{$a[0]}</td > 42 <td>{$v[4]}</td> 43 </tr> 44 "; 45 } 46 ?> 47 </table> 48 </table> 49 </body> 50 </html>