1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <?php
function my_exception_handler($exception)
{
die('Exception non gérée : '.$exception->getMessage());
}
class MyDB
{
public $db;
protected $tableName;
public function __construct($dbConfig, $tableConfig, $formAction = FALSE) {
$this->db = $dbConfig;
$this->tableName = $tableConfig;
}
}
set_exception_handler('my_exception_handler');
$pdo = new PDO('mysql:host=localhost;dbname=jeu_de_roles', 'root', '', array(PDO::ERRMODE_EXCEPTION));
$bdd = new MyDB($pdo, 'test');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statements = array(); // Contient tous les statements du script
$sql = 'SELECT id, name
FROM jdr_player';
$statements['players'] = $bdd->db->prepare($sql);
$sql = 'SELECT ps.stat_id AS id, s.name
FROM jdr_player_stat AS ps
INNER JOIN jdr_stat AS s ON ps.stat_id = s.id
WHERE ps.player_id = ?';
$statements['stats'] = $bdd->db->prepare($sql);
$statements['players']->execute();
$players = $statements['players']->fetchAll();
foreach($players as $player)
{
$statements['stats']->execute(array($player['id']));
$stats = array();
foreach($statements['stats']->fetchAll() as $stat)
{
$stats[] = $stat['name'];
}
echo $player['name'].' : '.implode(', ', $stats).'<br />';
}
?> |