实例
<?php //1.连接数据库 $link = mysqli_connect('localhost','root','123456'); //2.判断错误 if (mysqli_connect_errno($link)>0) { echo mysqli_connect_error($link);exit; } //3.选择数据库 mysqli_select_db($link,'ss34'); //4. 设置字符集 mysqli_set_charset($link,'utf8'); //5.准备sql语句 $sql = "SELECT id,name,sex,age,city FROM info"; //6.发送SQL语句 $result = mysqli_query($link,$sql); //7.处理结果集 if($result && mysqli_num_rows($result)>0){ //我们声明一个新的数组将拿出来的一维数组变为二维数组 $userlist = array(); //循环得到数据库里面的数据 while($row = mysqli_fetch_assoc($result)){ //var_dump($row); $userlist[]=$row; } } //var_dump($userlist); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <table border="1" width="800" align="center"> <tr> <td>编号</td> <td>姓名</td> <td>年龄</td> <td>性别</td> <td>城市</td> </tr> <?php foreach($userlist as $value){?> <tr> <td><?php echo $value['id']?></td> <td><?php echo $value['name']?></td> <td><?php echo $value['age']?></td> <td><?php echo $value['sex']?></td> <td><?php echo $value['city']?></td> </tr> <?php } ?> </table> </body> </html> <?php mysqli_close($link);?>
运行实例 »
点击 "运行实例" 按钮查看在线实例