· 10 years ago · Apr 27, 2016, 01:06 AM
1/*
2DROP TABLE IF EXISTS "usuario";
3CREATE TABLE "usuario" ("idUsuario" INTEGER PRIMARY KEY NOT NULL , "nome" VARCHAR, "email" VARCHAR NOT NULL , "senha" VARCHAR NOT NULL , "dataCriacao" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP);
4INSERT INTO "usuario" VALUES(1,'Gabriel','gabrielr47@gmail.com','110490','2016-04-26 11:15:23');
5INSERT INTO "usuario" VALUES(2,'Rodrigues','epic_bow@gmail.com','0f34eb03f5d7599dbca41895f081dce7','2016-04-26 12:29:42');
6*/
7// connection sqlite using pdo
8try {
9 $db = new PDO('sqlite:app.sqlite');
10 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION,PDO::FETCH_ASSOC);
11} catch (Exception $e) {
12 $e->getMessage();
13}
14
15// exemple dao usuarios
16
17<?php
18
19include 'db.php';
20
21class Usuario {
22
23 private $nome;
24 private $email;
25 private $senha;
26
27 public function __construct($nome, $email, $senha) {
28 try {
29 if (strlen($nome) < 1):
30 throw new Exception("Campo nome deve conter mais de uma letra.");
31 endif;
32 if (strlen($email) < 6):
33 throw new Exception("Campo email deve ser válido");
34 endif;
35 if (strlen($senha) < 6):
36 throw new Exception("Campo senha deve conter mais de 6 caracteres.");
37 endif;
38 $this->nome = $nome;
39 $this->email = $email;
40 $this->senha = md5($senha);
41 } catch (Exception $exc) {
42 echo $exc->getMessage();
43 }
44 }
45
46 public function getNome() {
47 return $this->nome;
48 }
49
50 public function getEmail() {
51 return $this->email;
52 }
53
54 public function getSenha() {
55 return $this->senha;
56 }
57
58}
59
60class UsuarioDao {
61
62 public static function insert(Usuario $u) {
63 global $db;
64 $result = $db->query('SELECT (MAX(idUsuario) + 1) idUsuario FROM Usuario');
65 foreach ($result as $r) {
66 $idUsuario = $r['idUsuario'];
67 }
68 if (isset($idUsuario)):
69 $sql = $db->prepare('INSERT INTO usuario (idUsuario, nome, email, senha) VALUES (?,?,?,?)');
70 $sql->bindParam(1, $idUsuario);
71 $sql->bindParam(2, $u->getNome());
72 $sql->bindParam(3, $u->getEmail());
73 $sql->bindParam(4, $u->getSenha());
74 $sql->execute();
75 endif;
76 }
77
78 public static function select() {
79 global $db;
80 $result = $db->query('SELECT * FROM Usuario');
81 var_dump($result->fetchAll());
82 }
83
84 public static function update() {
85
86 }
87
88 public static function delete() {
89
90 }
91
92}
93
94$user2 = new Usuario("Rodrigues", "epic_bow@gmail.com", "110490");
95//UsuarioDao::insert($user2);
96UsuarioDao::select();