测试PHP与MySQL数据库连接查询,新手


创建库 :create   database if not exists   web;
创建表 : create  table if not exists web.students(id int,name varchar(3),age int,birth datetime);
插入数据:insert  web.students values (1,"张三",18,now()),(1,"李四",21,now());


header("content-type:text/html;charset=utf-8");
$servername = "192.168.0.118";
$username = "root";
$passwd = "123456";

$conn = mysqli_connect($servername,$username,$passwd);

if(!$conn){
    die("connection failed:" . mysqli_connect_errno());
}else{
    $sql = "select * from web.students";
    $result = mysqli_query($conn,$sql);
     $arr_final=[];
    while($row = mysqli_fetch_assoc($result)) {
        $arr_final[]=$row;
    }
    print_r($arr_final);
    mysqli_free_result($result);
    echo "success";
    mysqli_close($conn);

}

?>

你可能感兴趣的:(数据库,php,mysql)