//造一个mysqli对象,造连接对象
$db = new MySQLi("localhost", "用户名", "密码", "dbname
");
//准备一条SQL语句
$sql = "select * from 表名";
//执行SQL语句,如果是查询语句,成功返回结果集对象
$result = $db->query($sql);
//判断返回是否执行内容
if ($result) {
//如果成功了,从结果集对象里面读取数据
// $attr = $result->fetch_all();
// $attr = $result->fetch_row();//执行一次读一行
// $attr = $result->fetch_row();
// $attr = $result->fetch_row();
// while ($attr = $result->fetch_row())//返回索引数组
// {
// var_dump($attr);
//
// $attr=$result->fetch_assoc();//返回关联数组
// $attr=$result->fetch_object();//返回对象
// $attr=$result->fetch_array();//返回的数据既有索引,又有关联的
// var_dump($attr);
}
形式1
<table border="1" width="100%" cellpadding="0" cellspacing="0"> <tr> <td>号码</td> <td>姓名</td> <td>性别</td> <td>生日</td> <td>职称</td> <td>部门</td> </tr> <?php $db=new MySQLi("localhost","root","root","zuoye"); $sql="select * from teacher"; $result =$db->query($sql); if($result) { while($attr=$result->fetch_row()) { echo"<tr> <td>{$attr[0]}</td> <td>{$attr[1]}</td> <td>{$attr[2]}</td> <td>{$attr[3]}</td> <td>{$attr[4]}</td> <td>{$attr[5]}</td> </tr>"; } } ?>
</table>
形式2
$db = new MySQLi("localhost","root","root","zuoye"); $sql = "select * from nation"; $result = $db->query($sql); echo "<select>"; if($result) { while($attr = $result->fetch_row()) { echo "<option value='{$attr[0]}'>{$attr[1]}</option>"; } } echo "</select>";