简体   繁体   中英

Class not found on AWS Elastic beanstalk environment

Im getting the follow error from my elastic beanstalk application:

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

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:

<?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".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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