site-accueil-insa/classes/dao.php

61 lines
1.6 KiB
PHP
Raw Normal View History

2018-06-15 09:33:29 +02:00
<?php
class Dao {
private $conn;
2018-06-15 11:12:55 +02:00
private $debug = false;
2018-06-15 09:33:29 +02:00
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){
2018-06-15 11:12:55 +02:00
$this->debug = true;
2018-06-15 09:33:29 +02:00
}
}
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){
2018-06-15 11:12:55 +02:00
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;
2018-06-15 09:33:29 +02:00
}
public function save_scores($scores_json, $team){
2018-06-15 11:12:55 +02:00
if (!$this->debug)
{
$sql = 'DELETE FROM scores WHERE team = ?';
2018-06-15 09:33:29 +02:00
$cursor = $this->conn->prepare($sql);
2018-06-15 11:12:55 +02:00
$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]);
}
2018-06-15 09:33:29 +02:00
}
}
}