php入门pdo的使用--增加数据,查询数据

 setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        echo $pdo -> getAttribute(PDO::ATTR_ERRMODE);
        $username = "jixue";
        $pwd = md5(12345);
        $email = "[email protected]";
        $sql = "INSERT INTO PERSON(username,pwd,email) VALUE('{$username}','{$pwd}','{$email}')";
        //exec只执行有影响行数的语句(通常指增,删,改) 
        $affected = $pdo -> exec($sql);
        //query执行有结果集的语句 (通常指查询)
        $sql = "SELECT * FROM PERSON";
        $stmt = $pdo -> query($sql);
        var_dump($stmt);
        echo "
"; var_dump($affected); echo "
"; foreach ($stmt as $key ) { echo "{$key['id']}-{$key['username']}-{$key['pwd']}-{$key['email']}-
"; } // var_dump($pdo); } catch(PDOException $e){ echo $e -> getMessage(); echo $e -> getFile(); echo $e -> getLine(); echo $e -> getCode(); } ?>

执行后页面显示:

上面代码打印输出

你可能感兴趣的:(php入门pdo的使用--增加数据,查询数据)