Bonjour et bonne année à tous,
J'ai un souci de connexion à ma bdd. J'ai créé un fichier inc_connexion.php sensé établir la connexion avec ma bdd et mon fichier index.php sauf que celui-ci ne le fait pas et me retourne un gentil message d'erreur.
Quand je test la requête seul dans l'utilitaire SQL, ça fonctionne sans problèmes.
Si je mets la même requête dans la variable query, plus rien fonctionne. Si je supprime la fin de ligne à partir du INNER JOIN, la ça fonctionne.
Voici le message d'erreur ;
Fatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in D:\xampp\htdocs\Departements\admin\index.php:34 Stack trace: #0 D:\xampp\htdocs\Departements\admin\index.php(34): PDO->query('SELECT villes_p...') #1 {main} thrown in D:\xampp\htdocs\Departements\admin\index.php on line 34
Voici les deux fichiers;
inc_connexion.php
et index.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
19
20
21
22
23
24
25
26
27
28
29
30
31
32 <?php class Database { /** * Déclarations variables connexion à la base de données */ private static $dbHost = "localhost"; private static $dbName = "db_france"; private static $dbUsername = "root"; private static $dbUserPassword = ""; private static $connection = NULL; public static function connect() { if (self::$connection == NULL) { try { self::$connection = new PDO("mysql:host=" . self::$dbHost . "; dbName =" . self::$dbName, self::$dbUsername, self::$dbUserPassword); self::$connection -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { die($e -> getMessage()); } } return self::$connection; } public static function disconnect() { self::$connection = NULL; } }
Si quelqu'un peut m'expliquer mon erreur, je l'en remercie d'avance.
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 <!doctype html> <html lang = "fr"> <head> <meta charset = "UTF-8"> <meta name = "viewport" content = "width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv = "X-UA-Compatible" content = "ie=edge"> <link rel="stylesheet" type="text/css" href="../css/styles.css"> <title>Administration</title> </head> <body> <main id="container"> <header id="head"> <h1 id="titre">Départements et régions de France</h1> </header> <section id="tableau"> <table> <caption>Liste des villes et régions</caption> <thead> <tr> <th>Ville</th> <th>Numéro</th> <th>Départements</th> <th>Régions</th> <th>Actions</th> </tr> </thead> <tbody> <?php require_once 'inc_connexion.php'; $db = Database::connect(); $statement = $db->query('SELECT villes_prefecture.nom_ville, villes_prefecture.texte_ville, villes_prefecture.sp_dept, regions.nom_region FROM villes_prefecture INNER JOIN regions ON regions.id = villes_prefecture.region'); while ($item = $statement->fetch()) { echo '<tr>'; echo '<td width="200">' . $item['nom_ville'] . '</td>'; echo '<td width="28">' . $item['numero'] . '</td>'; echo '<td width="200">' . $item['nom_ville'] . '</td>'; echo '<td width="300">' . $item['nom_region'] . '</td>'; echo '<td width="350">'; echo '<a class="bouton" href="view.php?id=' . $item['dept_id'] . '"> Voir</a>'; echo '<a class="bouton" href="update.php?id=' . $item['dept_id'] . '"> Modifier</a>'; echo '<a class="bouton" href="delete.php?id=' . $item['dept_id'] . '"> Supprimer</a>'; echo '</td>'; echo '</tr>'; } Database::disconnect(); ?> </tbody> </table> </section> </main> </body> </html>
Partager