Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Connect
- {
- private $host = "127.0.0.1"; //localhost
- private $database = "academia_hello_php";
- private $username = "root";
- private $password = "root";
- private $driver = "mysql";
- private $pdo;
- public function __construct()
- {
- try {
- $this->pdo = new PDO(
- "{$this->driver}:host={$this->host};{$this->database}",
- $this->username,
- $this->password,
- array(
- PDO::ATTR_PERSISTENT => true //não é fechada no final do script, e sim armazenada em cache
- )
- );
- $this->pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC); //retorna como array associativo
- $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Além de armazenar o código de erro, este tipo de manipulação de erro irá lançar uma exceção PDOException
- } catch (PDOException $e) {
- die($e->getMessage());
- }
- }
- public function select()
- {
- $stmt = $this->pdo->query('SELECT * FROM academia_hello_php.alunos');
- return $stmt->fetchAll(PDO::FETCH_ASSOC);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement