//index.php (绝对不能是.html格式,如果用ajax获取数据可以用html)
<?php
require_once 'functions.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<table width="50%" style="text-align: center;">
<thead><th>编号</th><th>姓名</th><th>年龄</th><th>性别</th></thead><tbody>
<?php
$coon =coonectDb();
$db=mysqli_select_db($coon, "myapp");
mysqli_query($coon, "set names 'utf8'");
$sql="select * from users";
$result=mysqli_query($coon, $sql);
$row=mysqli_num_rows($result);
for($i=0;$i<$row;$i++)
{
$info=mysqli_fetch_assoc($result);
$id=$info['id'];
$name=$info['name'];
$age=$info['age'];
$sex=$info['sex'];
echo "<tr> <td>$id</td> <td>$name</td> <td>$age</td> <td>$sex</td></tr>";
}
?>
</tbody></table>
</body>
</html>
//functions.php (类方法)
require_once 'config.php';
function coonectDb(){
return mysqli_connect(mysql_host, mysql_user, mysql_pw);
}
//config.php(基本配置)
<?php
define('mysql_host','localhost');
define('mysql_user','root');
define('mysql_pw','');
?>