1. //连接数据库方式1
    2. // $conn = new mysqli('localhost', 'root', 'password', 'test');
    3. //连接数据库方式2
    4. $conn = new mysqli();
    5. $conn -> connect('127.0.0.1', 'root', 'root', 'xc');
    6. //check connection (检查PHP是否连接上MYSQL)
    7. if ($conn -> connect_errno) {
    8. printf("Connect failed: %s\n", $conn->connect_error);
    9. exit();
    10. }
    11. //查询代码
    12. // $sql = "select * from name";
    13. $sql = "insert into name (name) values('谁谁谁啊')";
    14. $query = mysqli_query($conn,$sql);
    15. var_dump($query);
    16. // mysqli_fetch_row 返回数字key
    17. // mysqli_fetch_assoc 返回字段key
    18. // mysqli_fetch_array 多一倍,都有
    19. // 返回数据
    20. // while($row=mysqli_fetch_assoc($query)){
    21. // var_dump($row);
    22. // }
    23. //关闭MySQL连接
    24. $conn -> close();