Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DBConnection
- {
- const USERNAME = "root";
- const PASSWORD = "";
- const HOST = "localhost";
- const DB = "instituto";
- private function getConnection()
- {
- $username = self::USERNAME;
- $password = self::PASSWORD;
- $host = self::HOST;
- $db = self::DB;
- $connection = new PDO("mysql:dbname=$db;host=$host", $username, $password);
- return $connection;
- }
- public function queryList($sql, $args = null)
- {
- $connection = $this->getConnection();
- $stmt = $connection->prepare($sql);
- if (!empty($args))
- $stmt->execute($args);
- else
- $stmt->execute();
- return $stmt;
- }
- public function createTables()
- {
- $estudiante = "CREATE TABLE IF NOT EXISTS Estudiante (
- id INT(11) NOT NULL AUTO_INCREMENT,
- nombre VARCHAR(255),
- apellidos VARCHAR(255),
- email VARCHAR(255),
- telefono INT(9),
- tutor VARCHAR(255),
- grupo VARCHAR(255),
- fechaInicio TIMESTAMP
- )";
- $this->queryList($estudiante);
- }
- }
- /*
- * MODELO
- */
- class Estudiante
- {
- private $id;
- private $nombre;
- private $apellidos;
- private $email;
- private $telefono;
- private $tutor;
- private $grupo;
- private $fechaInicio;
- public function __construct($id, $nombre, $apellidos, $email, $telefono, $tutor, $grupo, $fechaInicio)
- {
- $this->id = $id;
- $this->nombre = $nombre;
- $this->apellidos = $apellidos;
- $this->email = $email;
- $this->telefono = $telefono;
- $this->tutor = $tutor;
- $this->grupo = $grupo;
- $this->fechaInicio = $fechaInicio;
- }
- public function guardar()
- {
- global $DBCon;
- $query = "INSERT INTO Estudiante VALUES (:id, :nombre, :apellidos, :email, :telefono, :tutor, :grupo, :fechaInicio)
- ON DUPLICATE KEY UPDATE nombre = :nombre, apellidos = :apellidos, email = :email, telefono = :telefono, grupo = :grupo";
- $args = array(":id" => $this->id, ":nombre" => $this->nombre, ":apellidos" => $this->apellidos, ":email" => $this->email,
- ":telefono" => $this->telefono, ":tutor" => $this->tutor, ":grupo" => $this->grupo, ":fechaInicio" => $this->fechaInicio);
- $stm = $DBCon->queryList($query, $args);
- return $stm;
- }
- }
- $DBCon = new DBConnection();
- $DBCon->createTables();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement