我从我的Elastic beantalk应用程序中收到了以下错误:
致命错误:未捕获的错误:在“ 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”的文件夹中。
谢谢大家!
答案 0 :(得分:0)
找到了解决方案!
$this->pdo = new PDO($dsn, $username, $password);
需要更改
$this->pdo = new \PDO($dsn, $username, $password);
如果有人确切知道为什么会发生这种情况,请分享