site-accueil-insa/classes/dao.php

64 lines
1.8 KiB
PHP
Executable file

<?php
class Dao
{
private $conn;
private $debug = true;
public function __construct($path_to_password)
{
if ($this->debug) {
$username = 'root';
// $password = $this->read_password($path_to_password);
$password ='';
$dsn = 'mysql:dbname=phpmyadmin;host=127.0.0.1';
} else {
$username = 'accueil_insa';
$password = $this->read_password($path_to_password);
$dsn = 'mysql:dbname=accueil_insa;host=127.0.0.1';
}
try {
$this->conn = new PDO($dsn, $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']);
} catch (PDOException $e) {
echo $e;
}
}
private function read_password($path_to_password)
{
if ($this->debug)
$real_path = $path_to_password . "includes/.htpassdb_debug";
else
$real_path = $path_to_password . "includes/.htpassdb";
$file = fopen($real_path, "r") or die("Unable to open file!");;
$password = fgets($file);
fclose($file);
return $password;
}
public function get_score_team($team)
{
$sql = 'SELECT text, points FROM scores WHERE team = ?';
$cursor = $this->conn->prepare($sql);
$cursor->execute([$team]);
return $cursor->fetchAll(PDO::FETCH_ASSOC);
}
public function save_scores($scores_json, $team)
{
$sql = 'DELETE FROM scores WHERE team = ?';
$cursor = $this->conn->prepare($sql);
$cursor->execute([$team]);
foreach ($scores_json as $value) {
$sql = 'INSERT INTO scores (text, points, team) VALUES (?, ?, ?)';
$cursor = $this->conn->prepare($sql);
$cursor->execute([$value['text'], $value['points'], $team]);
}
}
}