Bonsoir,
Je m'essaye aux fonctions avec un testqui fait appel à db.php qui commence par
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
34
35 function afficheUser($users){ $tab = "<table border=1><tr><th>thème</th><th>nom du fichier</th><th>nom du répertoire</th><th>descriptif</th><th>visualisatio</th><th>Actions</th></tr>"; foreach($users as $user){ $tab .= "<tr> <td>".$user['code']."</td> <td>".$user['rec_chemin']."</td> <td>".$user['rec_repertoire']."</td> <td>".$user['rec_contenu']."</td> <td><img style='max-width:100px' src='img/".$user['rec_repertoire']."/".$user['rec_chemin']."'></td>"; $tab .= "<td><form method='POST' action='php/deletimage.php'>"; $tab .= "<input type='image' src='img/delete_user.png' width='25'>"; $tab .= "<input type=hidden name='rec_id' value='".$user['rec_id']."'>"; $tab .= "</form>"; $tab .= "<form method='POST' action='php/formage.php'>"; $tab .= "<input type='image' src='img/modify_user.png' width='25'>"; $tab .= "<input type=hidden name='rec_id' value='".$user['rec_id']."'>"; $tab .= "<input type=hidden name='rec_chemin' value='".$user['rec_chemin']."'>"; $tab .= "<input type=hidden name='rec_repertoire' value='".$user['rec_repertoire']."'>"; $tab .= "<input type=hidden name='code' value='".$user['code']."'>"; $tab .= "<input type=hidden name='rec_contenu' value='".$user['rec_contenu']."'>"; $tab .= "</form>"; $tab .= "</td>"; } $tab .= "</tr></table>"; return $tab; } function main(){ include("db.php"); $users = getUsers(); echo afficheUser($users); } main(); ?>
Ça coince au niveau de la première function, apparament au niveau du "global", précision: la table existe
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 $VALEUR_hote = 'localhost';// serveur de la base de données (localhost en général) $VALEUR_nom_bd = '12.45_sitayou'; // nom de la base de données $VALEUR_user = '12345_webmaster';// nom d'utilisateur de la base de données $VALEUR_mot_de_passe = '12345-2019/'; // mot de passe $VALEUR_port='3306'; try { $db = new PDO('mysql:host='.$VALEUR_hote.';port='.$VALEUR_port.';dbname='.$VALEUR_nom_bd, $VALEUR_user, $VALEUR_mot_de_passe , array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); $db->exec("SET CHARACTER SET utf8"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); } catch(PDOException $e) { echo 'Erreur : '.$e->getMessage(); echo 'N° : '.$e->getCode(); } function getUsers(){ global $db; $select = $db->prepare('SELECT id, nom, prenom, email FROM personnes'); $select->execute(); return $select->fetchAll(); } function getUserById($id){ global $db; $select = $db->prepare('SELECT id, nom, prenom, email FROM personnes WHERE id='.$id); $select->execute(); $result = $select->fetchAll(); return $result[0]; } etc....
avec le message classique
Qu'est ce que j'ai raté?Fatal error: Uncaught Error: Call to a member function prepare() on null in /storage/ssd4/693/9052693/public_html/db.php:19 Stack trace: #0 /storage/ssd4/693/9052693/public_html/index.php(31): getUsers() #1 /storage/ssd4/693/9052693/public_html/index.php(35): main() #2 {main} thrown in /storage/ssd4/693/9052693/public_html/db.php on line 19
Merci d'avance
Partager