• 连接认证、设定字符集和选择数据库

<?php
//使用mysqli操作数据库 
$conn = @mysqli_connect('localhost','root','root');

//设置字符集
$sql = 'set names utf8';
$res = mysqli_query($conn,$sql);

//选择数据库
$sql = "use my_database";
$res = mysqli_query($conn,$sql);

  • 新增数据

<?php
//接连接认证
$sql = "insert into my_student1 values (null,'犬夜叉','男',200,5)";
$res = mysqli_query($conn,$sql);

//查看受影响的行数
echo mysqli_affected_rows($conn);

  • 修改数据:与新增数据一样

<?php
//接连接认证
$sql = "update my_student1 set age = 22 where id = 1";  #注意筛选条件
$res = mysqli_query($conn,$sql);

//查看受影响的行数
echo mysqli_affected_rows($conn);

  • 删除数据:与新增和修改一样

<?php
//接连接认证
$sql = "delete from my_student1 where id = 1";
$res = mysqli_query($conn,$sql);

//查看受影响的行数
echo mysqli_affected_rows($conn);

  • 查询数据

<?php
//接连接认证
    
$sql = "select * from my_student1";
$res = mysqli_query($conn,$sql);                #此时$res是结果集

//读取一条数据
$row = mysqli_fetch_assoc($res);                #获取一条记录,且指针下移(下次获取下一条)

//取出全部
$lists = array();
while($row = mysqli_fetch_assoc($res)){
    $lists[] = $row;                            #二维数组保存
}