I am currently trying to learn how to use docker-compose crearting a LAMP stack with apache, php and mariadb and have ran into this error while trying to link to my database through PHP.
The error
Fatal error: Uncaught PDOException: could not find driver in /var/www/html/index.php:5 Stack trace: #0 /var/www/html/index.php(5): PDO->__construct('mysql:host=data...', 'admin', 'password') #1 {main} thrown in /var/www/html/index.php on line 5"
The error seems to be with my use of PDO, do I have the hostname set wrong?
PHP file:
<?php
$value = "World";
$db = new PDO('mysql:host=database;dbname=db;charset=utf8mb4', 'admin',
'password');
$databaseTest = ($db->query('SELECT * FROM test'))-
fetchAll(PDO::FETCH_OBJ);
?>
<html>
<body>
<h1>Hello, <?= $value ?>!</h1>
<?php foreach($databaseTest as $row): ?>
<p>Hello, <?= $row->name ?></p>
<?php endforeach; ?>
</body>
</html>
Docker-Compose.yml
version: '3'
services:
php-apache:
image: php:7.2.1-apache
ports:
- 80:80
volumes:
- ./DocumentRoot:/var/www/html:z
links:
- 'mariadb'
mariadb:
image: mariadb:10.1
volumes:
- mariadb:/var/lib/mysql
- ./db/data.sql:/docker-entrypoint-initdb.d/data.sql
environment:
TZ: "Europe/London"
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
MYSQL_ROOT_PASSWORD: "password"
MYSQL_USER: 'admin'
MYSQL_PASSWORD: 'password'
MYSQL_DATABASE: 'db'
volumes:
mariadb:
db:
The data file:
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`name` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
LOCK TABLES `test` WRITE;
INSERT INTO `test` VALUES ('George'),('Sam'),('Kathy');
UNLOCK TABLES;
The webserver is running if I check with php info().
Could anyone offer any assistance with this?
Thanks
Scott