简体   繁体   English

在 AWS Elastic beanstalk 环境中找不到 Class

[英]Class not found on AWS Elastic beanstalk environment

Im getting the follow error from my elastic beanstalk application:我从我的弹性 beanstalk 应用程序中收到以下错误:

Fatal error: Uncaught Error: Class 'App\PDO' not found in /var/app/current/crawler/app/SQLConnection.php:32 Stack trace: #0 /var/app/current/crawler/init.php(18): App\SQLConnection->connect() #1 {main} thrown in /var/app/current/crawler/app/SQLConnection.php on line 32致命错误:未捕获错误:Class 'App\PDO' not found in /var/app/current/crawler/app/SQLConnection.php:32 堆栈跟踪:#0 /var/app/current/crawler/init.php(18 ): App\SQLConnection->connect() #1 {main} 在第 32 行抛出 /var/app/current/crawler/app/SQLConnection.php

The app runs perfectly locally (as is usually the case with errors).该应用程序在本地完美运行(通常会出现错误)。 All libraries and dependencies are included in the app I uploaded.所有库和依赖项都包含在我上传的应用程序中。

Can anyone see where I went wrong please?谁能看到我哪里出错了?

The initialzation script to create DB tables:创建数据库表的初始化脚本:

<?php 
require 'vendor/autoload.php'; 
require (__DIR__.'/app/SQLConnection.php');
require (__DIR__.'/app/SQLCreateTable.php');
require (__DIR__.'/app/SQLInsert.php');
require (__DIR__.'/app/SQLRetrieve.php');
require (__DIR__.'/app/SQLDelete.php');
use App\SQLConnection;
use App\SQLCreateTable;
use App\SQLInsert;
use App\SQLRetrieve;
use App\SQLDelete;

try {

    //connect to DB
    $sql = new SQLCreateTable((new SQLConnection())->connect());
    //get list of tables
    $tables = $sql->getTableList();
    //check if required tables excist
    if(in_array('results',$tables) && in_array('logs',$tables)){
        echo "Tables already created. You will be redirected to the home page"; 
        header( "refresh:5;url=/index.php" );
    }else {
        //create tables
        try {
            $sql->createTables();
            echo "Tables succesfully created! You will be redirected to the home page";
            header( "refresh:5;url=/index.php" );
        } catch (\PDOException $e) {
            echo "Failed to create tables: " . $e->getMessage();
        }

    }

} catch (\PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

Then the SQL connection script:然后是SQL连接脚本:

<?php
namespace App;

/**
 * SQL connnection
 */
class SQLConnection {
    /**
     * PDO instance
     * @var type 
     */
    private $pdo;

    /**
     * return in instance of the PDO object that connects to the SQLite database
     * @return \PDO
     */
    public function connect() {

        $dbhost = $_SERVER['RDS_HOSTNAME'];
        $dbport = $_SERVER['RDS_PORT'];
        $dbname = $_SERVER['RDS_DB_NAME'];
        $charset = 'utf8' ;

        $dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname};charset={$charset}";
        $username = $_SERVER['RDS_USERNAME'];
        $password = $_SERVER['RDS_PASSWORD'];

        if ($this->pdo == null) {
            try {
                // $this->pdo = new \PDO("mysql:host=localhost;dbname=crawler", "root", "");
                $this->pdo = new PDO($dsn, $username, $password);
                // set the PDO error mode to exception
                $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
            } catch (\PDOException $e) {
               echo "Connection failed: " . $e->getMessage();
            }
        }
        return $this->pdo;
    }


}

The 'Init' script is at root level and the SQLconnection script is in a folder called "app". “Init”脚本位于根级别,SQLconnection 脚本位于名为“app”的文件夹中。

Thanks guys!多谢你们!

Found the solution!找到了解决方案!

$this->pdo = new PDO($dsn, $username, $password);

Needs to change需要改变

$this->pdo = new \PDO($dsn, $username, $password);

If anyone knows exactly why this is the case, please share如果有人确切知道为什么会这样,请分享

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM