数据库访问(现用基本格式)

面向对象的方式访问数据库

1.造对象
  $db=new MySQLi("localhost","root","123","hr");

2.判断连接是否出错
  !mysqli_connect_error() or die("连接失败!");

3.写SQL语句
  $sql="inselt into info values('p005','夏天','1','n003','1997-5-10')";  //添加数据进入数据库

4.执行SQL语句,查询语句返回结果集对象,其他语句返回TRUE或FALSE
  $result=$db->query($sql);

5.从结果集对象中读取数据
  $attr = $result->fetch_all(MYSQLI_BOTH);   //以二维数组的方式返回所有数据
  $attr = $result->fetch_array();   //返回当前指针指向的这条数据
  $attr = $result->fetch_assoc();   //返回当前指针指向的这条数据(关联数组)
  $attr = $result->fetch_object();   //返回对象
  $attr = $result->fetch_row();   //返回索引数组
var_dump($attr);

while($attr=$result->fetch_row())    //while循环
{
  var_dump($attr);
}

原文地址:https://www.cnblogs.com/m-m-g-y0416/p/5582112.html