52 lines
1.1 KiB
PHP
52 lines
1.1 KiB
PHP
<?php
|
|
class DB {
|
|
private $server = 'localhost';
|
|
private $user= 'root';
|
|
private $pass= 'ff9800s_a_d';
|
|
private $db= 'alumnos';
|
|
|
|
private $options = [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
];
|
|
|
|
private $dsn;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->dsn = "mysql:host=".$this->server.";dbname=".$this->db;
|
|
}
|
|
|
|
public function query($query, ...$params)
|
|
{
|
|
try
|
|
{
|
|
$pdo = new PDO($this->dsn, $this->user, $this->pass, $this->options);
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute($params);
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
catch(PDOException $e)
|
|
{
|
|
throw new \PDOException($e->getMessage(), (int)$e->getCode());
|
|
}
|
|
}
|
|
|
|
public function dml($dml, ...$params)
|
|
{
|
|
try
|
|
{
|
|
$pdo = new PDO($this->dsn, $this->user, $this->pass, $this->options);
|
|
$stmt = $pdo->prepare($dml);
|
|
$stmt->execute($params);
|
|
return $stmt->rowCount();
|
|
}
|
|
catch(PDOException $e)
|
|
{
|
|
throw new \PDOException($e->getMessage(), (int)$e->getCode());
|
|
}
|
|
}
|
|
}
|
|
?>
|