· 8 years ago · Nov 27, 2017, 08:04 AM
1<?php
2
3class DBConnection
4{
5 const USERNAME = "root";
6 const PASSWORD = "";
7 const HOST = "localhost";
8 const DB = "instituto";
9
10 private function getConnection()
11 {
12 $username = self::USERNAME;
13 $password = self::PASSWORD;
14 $host = self::HOST;
15 $db = self::DB;
16 $connection = new PDO("mysql:dbname=$db;host=$host", $username, $password);
17 return $connection;
18 }
19
20 public function queryList($sql, $args = null)
21 {
22 $connection = $this->getConnection();
23 $stmt = $connection->prepare($sql);
24 if (!empty($args))
25 $stmt->execute($args);
26 else
27 $stmt->execute();
28 return $stmt;
29 }
30
31 public function createTables()
32 {
33 $estudiante = "CREATE TABLE IF NOT EXISTS Estudiante (
34 id INT(11) NOT NULL AUTO_INCREMENT,
35 nombre VARCHAR(255),
36 apellidos VARCHAR(255),
37 email VARCHAR(255),
38 telefono INT(9),
39 tutor VARCHAR(255),
40 grupo VARCHAR(255),
41 fechaInicio TIMESTAMP
42 )";
43 $this->queryList($estudiante);
44 }
45}
46
47/*
48 * MODELO
49 */
50
51class Estudiante
52{
53 private $id;
54 private $nombre;
55 private $apellidos;
56 private $email;
57 private $telefono;
58 private $tutor;
59 private $grupo;
60 private $fechaInicio;
61
62 public function __construct($id, $nombre, $apellidos, $email, $telefono, $tutor, $grupo, $fechaInicio)
63 {
64 $this->id = $id;
65 $this->nombre = $nombre;
66 $this->apellidos = $apellidos;
67 $this->email = $email;
68 $this->telefono = $telefono;
69 $this->tutor = $tutor;
70 $this->grupo = $grupo;
71 $this->fechaInicio = $fechaInicio;
72 }
73
74 public function guardar()
75 {
76 global $DBCon;
77 $query = "INSERT INTO Estudiante VALUES (:id, :nombre, :apellidos, :email, :telefono, :tutor, :grupo, :fechaInicio)
78 ON DUPLICATE KEY UPDATE nombre = :nombre, apellidos = :apellidos, email = :email, telefono = :telefono, grupo = :grupo";
79 $args = array(":id" => $this->id, ":nombre" => $this->nombre, ":apellidos" => $this->apellidos, ":email" => $this->email,
80 ":telefono" => $this->telefono, ":tutor" => $this->tutor, ":grupo" => $this->grupo, ":fechaInicio" => $this->fechaInicio);
81 $stm = $DBCon->queryList($query, $args);
82 return $stm;
83 }
84}
85
86$DBCon = new DBConnection();
87$DBCon->createTables();