site-accueil-insa/classes/dao.php
2018-07-06 19:34:49 +02:00

60 lines
1.6 KiB
PHP
Executable file

<?php
class Dao {
private $conn;
private $debug = false;
public function __construct($path_to_password)
{
$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){
$this->debug = true;
}
}
private function read_password($path_to_password){
$file = fopen($path_to_password."includes/.htpassdb", "r") or die("Unable to open file!");;
$password = fgets($file);
fclose($file);
return $password;
}
public function get_score_team($team){
if (!$this->debug)
{
$sql = 'SELECT text, points FROM scores WHERE team = ?';
$cursor = $this->conn->prepare($sql);
$cursor->execute([$team]);
return $cursor->fetchAll(PDO::FETCH_ASSOC);
}
else
return 0;
}
public function save_scores($scores_json, $team){
if (!$this->debug)
{
$sql = 'DELETE FROM scores WHERE team = ?';
$cursor = $this->conn->prepare($sql);
$cursor->execute([$team]);
$array = json_decode($scores_json)->array;
foreach ($array as $value){
$sql = 'INSERT INTO scores (text, points, team) VALUES (?, ?, ?)';
$cursor = $this->conn->prepare($sql);
$cursor->execute([$value->text, $value->points, $team]);
}
}
}
}