Bonjour à tous.
Débutant en PHP
Là actuellement, j'apprends le MVC et je ne comprends pas la raison de mes erreurs
Notice: Undefined variable: cnx in C:\wamp64\www\Exercices\index.php on line 4
et Fatal error: Call to a member function prepare() on null in C:\wamp64\www\Exercices\model.php on line 6
fichier cnx.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 <?php $dsn='mysql:host= localhost;dbname=mvc;charset=utf-8'; $user='root'; $pass=''; try { $cnx = new PDO($dsn, $user, $pass); } catch(Exception $e) { echo'Un probleme est survenue'; } ?>
fichier model.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <?php function AfficherTout($cnx){ $sql='SELECT * FROM contact'; $req= $cnx->prepare($sql); $req->execute(); return $req; }
fichier index.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 <?php include('cnx.php'); include('model.php'); $req=AfficherTout($cnx); include('view.php');
fichier view.php
.fichier style.css
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <!doctype html> <html lang="fr"> <head> <meta charset="utf-8"> <title>Titre de la page</title> <link rel="stylesheet" href="style.css"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <div class='box'> <h1> Listes des contactes</h1> <ol> <?php while(data = $req->fetch(PDO::FETCH_ASSOC)){ ?> <li><?= ucfirst($data['prenom']); ?><?= ucfirst($data['nom']); ?></li> <?php } ?> </ol> </div> </body> </html>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 box{ width:40px; margin:auto; font-family:arial; font-size:16px; }
Merci d'avance pour vos réponses.
Partager