请参考 天龙八"步",知识点,这里比较粗浅,勿喷!
<meta charset="UTF-8">
<?php
//1.配置数据的基本信息
$servername = "127.0.0.1";
$username = "root";
$password = "";
$database = "php";
$port = "3306";
//2.连接数据库
//$conn=mysqli_connect("主机","用户名","密码","数据库","端口号");
$conn = mysqli_connect($servername, $username, $password, $database, $port);
//3.检查连接
if (!$conn) {
die("连接数据库失败->" . mysqli_connect_error());
} else {
echo "连接成功";
//4.选择一个需要操作的数据库 -
//因为上面我们已经确定了连接的数据所以可以省略该步骤
//mysqli_select_db($database,$conn);
//5.设置字符集
mysqli_set_charset($conn, "utf8");
//6.定义SQL指令
$sql = 'select * from sys_user';
//7.执行SQL指令
$resultSet = mysqli_query($conn, $sql);
//处理结果集
//var_dump(mysqli_fetch_array($resultSet));
//数组迭代方式
/* $data = mysqli_fetch_array($resultSet);
foreach ($data as $key => $value) {
echo $key . '=' . $value . '<br/>';
}*/
//个人推荐方式
while (list($id, $account, $password, $user_name) = mysqli_fetch_row($resultSet)) {
echo $id . '--' . $account . '--' . $password. '--' . $user_name. '<br/>';
}
//8.释放资源
mysqli_close($conn);
}
//$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die(‘Unable to connect, please check connection paremeters’);
?>