<?php

header('Content-Type: text/html; charset=UTF-8');

try {
    $dsn = 'mysql:host=127.0.0.1;port=3306;dbname=test;charset=utf8';
    $pdo = new PDO($dsn, 'root', 'root');
} catch (PDOException $e) {
    die($e->getMessage());
}

$sql = "INSERT INTO user (username,nickname,password,sex) VALUES ('admin','admin','123456',1)";
$info = $pdo->exec($sql); // 返回数值(插入多行好像也是放回1,修改和删除返回影响行数)
$row_num = $pdo->LastInsertId(); // 获取最后插入的ID(修改和删除返回0)

$sql = "SELECT * FROM user";
$result = $pdo->query($sql); // 返回一个对象
//$oneData = $result->fetch(); //返回一行数据
//$allData = $result->fetchAll(); //返回所有数据