1. 面向对象连接

    //第一步连接数据库
    $link = new mysqli ('localhost','root','root');
    //第二步选择数据库
    $link->select_db('dbname');
    //第三步设置字符集
    $link->set_charset('utf8');
    //第四步执行操作
    $result=$link->query('select * from favorite');
    //$sql="insert into user(name,gender,age) values('小明',1,22)";
    //$sql="update user set name='%s' where id=5";
    //$sql="delete from user where id=%d";
    $row = $result->fetch_all();
    //fetch_assoc()查询到的一条数据以关联数组的形式返回
    //fetch_row()查询到的一条数据以索引数组的形式返回
    //fetch_array()查询到的一条数据以索引数组和关联数组混合形式返回
    //$row = $result->fetch_assoc();
    //$row = $result->fetch_row();
    //$row = $result->fetch_array();
    //$num_rows = $result -> num_rows;取得结果集中行的数目
    //printf ("%s : %s",$row["name"],$row["url"]);
    echo "<pre>";
    var_dump($row);
    echo "</pre>";
    //第五步释放结果集
    $result->close();
    //第六步关闭连接
    $link->close();
  2. 面向过程连接

    $link=mysqli_connect('localhost','root','root');
    mysqli_select_db($link,'dbname');
    $result=mysqli_query($link,'select * from favorite');
    $row=mysqli_num_rows($result);
    var_dump($row);
    mysqli_close($link);