在Docker容器中的PHP文件中找不到mysqli类错误

时间:2018-12-07 22:32:08

标签: php mysql database docker pdo

所以我正在docker容器中运行Web应用程序。我正在使用命令“ docker-compose up”创建图像和容器。每当我尝试访问网页并按“ CTRL + U”时,都会出现以下错误:

'致命错误:未捕获错误:在/var/www/html/mysql.php中找不到类'mysqli':7堆栈跟踪:#0 {main}抛出在/var/www/html/mysql.php上第7行”

,当我使用PDO时,出现以下错误: “连接失败:找不到驱动程序”。

我具有以下地图结构

dockerized-lamp (Directory)
->docker-compose.yml (File)
->DocumentRoot (Directory)
  ->PHP files
->php-apache (Directory)
  ->DockerFile

我的Yml文件包含:

version: '3'
services:
    php-apache:
        image: php:7.2.1-apache
        container_name: phpapache
        ports:
        - 80:80
    volumes:
        - ./DocumentRoot:/var/www/html:z
    links:
        - 'mariadb'

mariadb:
    image: mariadb:10.1
    container_name: mariadb
    volumes:
        - mariadb:/var/lib/mysql
    environment:
        TZ: "Europe/Brussel"
        MYSQL_ALLOW_EMPTY_PASSWORD: "no"
        MYSQL_ROOT_PASSWORD: "password"
        MYSQL_USER: 'user'
        MYSQL_PASSWORD: 'password'
        MYSQL_DATABASE: 'db'

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    environment:
        - PMA_ARBITRARY=1
        - PMA_HOST=mariadb
    restart: always
    ports:
        - 8080:80
    volumes:
        - /sessions
    links:
        - mariadb

volumes:
    mariadb:

我的DockerFile包含:

FROM php:7.2.1-apache
RUN docker-php-ext-install mysqli pdo pdo_mysql mysql

和我的PHP文件尝试连接到数据库:

<?php
$servername = "server";
$username = "root";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

我的PDO PHP文件是:

<?php
$servername = "localhost";
$username = "root";
$password = "password";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, 
$password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>

我试图在ubuntu服务器18.0.x和Debian Stretch上运行它们。

0 个答案:

没有答案