<?php header("content-type:text/html;charset=UTF-8"); $mysqli=new mysqli("localhost","root",""); if($mysqli->connect_errno){ die("connect error".$mysqli->connect_error); } $mysqli->set_charset("utf8"); $mysqli->select_db("xsphp"); $sql="select id,username,password,age from user"; $mysqli_result=$mysqli->query($sql); if($mysqli_result && $mysqli_result->num_rows>0){ while($row=$mysqli_result->fetch_assoc()){ $rows[]=$row; } } // print_r($rows); ?> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <h2>用户列表-<a href="adduse.php">添加用户</a></h2> <table border="1" width=80% bgcolor="pink" cellpadding="0" cellspacing="0"> <?php $i=1;foreach ($rows as $val):?> <tr> <td><?php echo $i;?></td> <td><?php echo $val['username'];?></td> <td><?php echo $val['password'];?></td> <td><a href="edituser.php">更新</a>|<a href="doAction">删除</a></td> </tr> <?php endforeach;?> </table> </body> </html>
慕课上的一个案例,其中
<?php $i=1;foreach ($rows as $val):?>
foreach循环,php语句中最后用冒号,而不用分号?如果分号只能调用出一条,说明报错了。
qq_大寒_2