第一种方法
conn1.php
<?php
//对象链接方法
$mysqli=new mysqli("localhost","root","root","test");
//$mysqli->select_db("test");
if($mysqli->connect_errno){
die("链接错误".$mysqli->connect_errno);
}
$mysqli->query("set names utf8");
?>
index1.php
<?php
include("conn1.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<table border="1">
<thead>
<tr>
<td>id</td>
<td>name</td>
<td>pwd</td>
<td>type</td>
</tr>
</thead>
<tbody>
<?php
$result=$mysqli->query("select * from user");
while($row=$result->fetch_assoc()){
?>
<tr>
<td><?php echo $row["id"] ?></td>
<td><?php echo $row["uname"] ?></td>
<td><?php echo $row["upwd"] ?></td>
<td><?php echo $row["utype"] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
运行结果
第二种方法
conn2.php
<?php
//ob_start();
//session_start();
$conn=mysql_connect("localhost","root","root");
mysql_select_db("test",$conn);
mysql_query("set names utf8");
?>
index2.php
<?php
include("conn2.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<table border="1">
<thead>
<tr>
<td>id</td>
<td>name</td>
<td>pwd</td>
<td>type</td>
</tr>
</tbody>
<tbody>
<?php
$result=mysql_query("select * from user");
while($row=mysql_fetch_assoc($result)){
?>
<tr>
<td><?php echo $row["id"] ?></td>
<td><?php echo $row["uname"] ?></td>
<td><?php echo $row["upwd"] ?></td>
<td><?php echo $row["utype"] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
运行结果