-
-
Save esilvajr/e364424959a5a095dc56f1a21041ceb8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/esilvajr/7ff6ec23af4728fe205a79133377f043