<?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){ echo '<table border ="1" width="800" align="center">'; echo '<tr>'; echo '<td>编号</td>'; echo '<td>姓名</td>'; echo '<td>年龄</td>'; echo '<td>性别</td>'; echo '<td>城市</td>'; echo '</tr>'; while($row = mysqli_fetch_assoc($result)){ echo '<tr>'; echo '<td>'.$row['id'].'</td>'; echo '<td>'.$row['name'].'</td>'; echo '<td>'.$row['age'].'</td>'; echo '<td>'.$row['sex'].'</td>'; echo '<td>'.$row['city'].'</td>'; echo '</tr>'; } echo '</table>'; } mysqli_close($link);
点击 "运行实例" 按钮查看在线实例