Essai un MVC PHP (pdo et objet)
Bonjour.
Je tente de créer un MVC très basique d'un simple affichage.
MVC PHP qui utilise PDO et Objet. J'ai besoin d'un bon guide.
Note: j'ai pas mis la balise code pour l'erreur pour faciliter la lecture.
Je crois bien que c'est du côté model.php mon problème.
Je sais que mon MVC ne doit pas être au Top.
fichier index.php:
Code:
1 2 3 4 5
|
<?php
include_once('bdd.php');
include_once('controler.php');
?> |
fichier bdd.php:
Code:
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
|
<?php
define('USERNAME2','root');
define('PASSWORD2','');
define('DSN2',"mysql:host=localhost;dbname=ptg");
class pdo_mysql {
private static $_instance;
public static function &pdo_connection() {
if (!self::$_instance) {
try {
self::$_instance = new PDO(DSN2, USERNAME2, PASSWORD2);
self::$_instance->setAttribute(PDO::ATTR_PERSISTENT,true);
self::$_instance->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch (PDOException $est) {
die("pdo connection error! ". $est->getMessage() ."<br/>");
}
}
return self::$_instance;
}
private function __construct() {
}
private function __clone() {
}
}
?> |
Il semble que j'ai un problème avec ma fonction artiste().
L'erreur produite:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BY id_artiste DESC LIMIT NULL, NULL' at line 1' in D:\www\b\model.php on line 36.
fichier controler.php:
Code:
1 2 3 4 5 6 7 8 9
|
<?php
include_once('model.php');
$artiste = new Artiste();
$artiste = $artiste->artiste(0, 1);
include_once('vue.php');
?> |
fichier model.php:
Code:
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
|
<?php
include_once('bdd.php');
abstract class Model extends pdo_mysql {
private $_conn;
protected $_pk;
private $offset;
private $limit;
public function __construct() {
$this->_conn = pdo_mysql::pdo_connection();
}
public function getOffset(){
return $this->offset;
}
public function setOffset($offset){
$this->offset = (int) $offset;
}
public function getLimit(){
return $this->limit;
}
public function setLimit($limit){
$this->limit = (int) $limit;
}
public function artiste($offset, $limit) {
$stmt = $this->_conn->prepare("SELECT * FROM ". $this->table ."ORDER BY id_artiste DESC LIMIT :offset, :limit");
$stmt->bindParam(':offset', $this->offset, PDO::PARAM_INT);
$stmt->bindParam(':limit', $this->limit, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
}
class Artiste extends Model {
protected $table='artiste';
public function __construct()
{
parent::__construct();
$this->_pk = 'id_artiste';
}
}
?> |
fichier vue.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
<!DOCTYPE html>
<html>
<head>
<title>Liste des Artistes</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Liste des Artistes</h1>
<?php
foreach($artiste as $a)
{
?>
<div class="news">
<?php echo $a->nom_artiste; ?>
<?php echo $a->prenom_artiste; ?>
</div>
<?php
}
?>
</body>
</html> |