Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:
使用mysqli类的对象去连接数据库,然后获取到mysqli_result对象,使用mysqli _result对象中的成员方法或者属性将获得的结果集中的数据(二维数组)遍历出来
<?php
$mysqlilink = [
'localhost' => '127.0.0.1', // 服务器地址
'database' => 'admin', // 数据库名
'username' => 'admin', // 用户名
'password' => 'huicheng123', // 密码
'hostport' => '3306', // 端口
];
$mysqli = new mysqli($mysqlilink['localhost'],$mysqlilink['username'],$mysqlilink['password'],$mysqlilink['database'],$mysqlilink['hostport']);
//检查连接是否成功
if($mysqli->connect_error)
{
//如果没有错误,会返回一个null值
die("连接失败,请重试:" . $mysqli->connect_error);//打印错误信息
}
//设置默认的客户端字符集
$myaqli->set_charset('utf-8');
// sql插入语句
$sql = "INSERT INTO users(`name`, `email`, `password`,`time`) VALUES ('小明', 'xiaomin@php.cn','123456','15133383837');";
$res = $mysqli->query($sql);//曾删
// $mysqli->query()只能执行单条查询
// sql更新语句
$sql = "UPDATE `users` SET `age`=21 WHERE `id`=12";
$res = $mysqli->query($sql);
// sql删除语句
$sql = "DELETE FROM `users` WHERE `name` = '灭绝师太' ";
$res = $mysqli->query($sql);
// sql查询语句(查询所有字段)
$sql = "SELECT * FROM `users`";
$res = $mysqli->query($sql);
// 使用mysql_result类的fetch_all方法,以数组的方式返回结果集;
$res = $res->fetch_all(MYSQLI_ASSOC);
// 遍历结果集
foreach($res as $key => $vulue)
{
echo "----{$vulue['id']}----{$vulue['name']}----{$vulue['email']}----{$vulue['time']}----
";
}