Connexion à la base de données avec PDO

Fermé
a.b - 10 avril 2021 à 16:03
yg_be Messages postés 22730 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 avril 2024 - 10 avril 2021 à 16:32
Bonjour,

Actuellement, je suis une série de cours sur la POO. Mais je suis bloqué. Je veux connecter à la base de données avec PDO avec le fichier ci-dessous :

/*
 * SETTINGS!
 */
$databaseName = 'oo_battle';
$databaseUser = 'root';
$databasePassword = '';

/*
 * CREATE THE DATABASE
 */
$pdoDatabase = new PDO('mysql:host=localhost', $databaseUser, $databasePassword);
$pdoDatabase->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdoDatabase->exec('CREATE DATABASE IF NOT EXISTS oo_battle');

/*
 * CREATE THE TABLE
 */
$pdo = new PDO('mysql:host=192.168.64.2;dbname='.$databaseName, $databaseUser, $databasePassword);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// initialize the table
$pdo->exec('DROP TABLE IF EXISTS ship;');

$pdo->exec('CREATE TABLE `ship` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
 `weapon_power` int(4) NOT NULL,
 `jedi_factor` int(4) NOT NULL,
 `strength` int(4) NOT NULL,
 `team` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci');

/*
 * INSERT SOME DATA!
 */
$pdo->exec('INSERT INTO ship
    (name, weapon_power, jedi_factor, strength, team) VALUES
    ("Jedi Starfighter", 5, 15, 30, "rebel")'
);
$pdo->exec('INSERT INTO ship
    (name, weapon_power, jedi_factor, strength, team) VALUES
    ("CloakShape Fighter", 2, 2, 70, "rebel")'
);
$pdo->exec('INSERT INTO ship
    (name, weapon_power, jedi_factor, strength, team) VALUES
    ("Super Star Destroyer", 70, 0, 500, "empire")'
);
$pdo->exec('INSERT INTO ship
    (name, weapon_power, jedi_factor, strength, team) VALUES
    ("RZ-1 A-wing interceptor", 4, 4, 50, "empire")'
);

echo "Ding!\n";




Mais lorsque j'exécute voilà ce qui m'afficher :


Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /Users/alfa/Documents/autres/code-oo/start/init_db.php:13 Stack trace: #0 /Users/alfa/Documents/autres/code-oo/start/init_db.php(13): PDO->__construct('mysql:host=loca...', 'root', '') #1 {main} thrown in /Users/alfa/Documents/autres/code-oo/start/init_db.php on line 13

Une aide serait le bienvenue :)

Je vous remercie.

Configuration: Macintosh / Safari 14.0.2
A voir également:

1 réponse

yg_be Messages postés 22730 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 26 avril 2024 1 477
10 avril 2021 à 16:32
bonjour,
je pense que ce n'est un problème dans ton code, ce serait plus tôt un problème dans l'installation du logiciel sur l'ordi.
0