-
-
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
create database
academia_hello_php
;use
academia_hello_php
;create table alunos
(
id int(11) auto_increment,
nome varchar(200),
nota double,
PRIMARY KEY (id)
);
insert into alunos (nome, nota) values
('Bart. Simpsons', 0),
('Lisa Simpsons', 10),
('Milhouse Van Houten', 7.5),
('Ralph Wiggum', 8),
('Nelson Muntz', 4),
('Martin Price', 9.5),
('Sherri', 7),
('Sherri e Terri', 6),
('Michael D Amico', 9.1),
('Corky James Jimbo Jones', 3.5);
create table diretor
(
id int(11) auto_increment,
nome varchar(200),
username varchar(200),
passwd varchar(200),
PRIMARY KEY (id)
);
insert into diretor (nome, username, passwd) values
('W. Seymour Skinner', 'skinner', md5('123mudar'));