在AWS Elastic beantalk环境中找不到类

问题描述 投票:0回答:1

我从我的弹性beanstalk应用程序中获取了跟随错误:

致命错误:未捕获的错误:在“ App \ PDO”类中找不到/var/app/current/crawler/app/SQLConnection.php:32堆栈跟踪:#0/var/app/current/crawler/init.php(18):App \ SQLConnection-> connect()#1{main}抛出于/var/app/current/crawler/app/SQLConnection.php第32行

该应用在本地完美运行(通常会出现错误)。我上载的应用程序中包含所有库和依赖项。

任何人都可以看到我哪里出了问题吗?

用于创建数据库表的初始化脚本:

<?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();
}

然后是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;
    }


}

'Init'脚本位于根级别,SQLconnection脚本位于名为“ app”的文件夹中。

谢谢你们!

php amazon-web-services pdo amazon-elastic-beanstalk
1个回答
0
投票

找到解决方案!

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

需要更改

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

如果有人确切知道为什么会发生这种情况,请分享

© www.soinside.com 2019 - 2024. All rights reserved.