douou1891 2019-02-28 22:09
浏览 105

致命错误:使用-Docker组合在LAMP堆栈上的未捕获PDOexception

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答