【发布时间】:2018-04-02 16:25:59
【问题描述】:
以下代码:
class Database {
(...)
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
(...)
}
在我的在线服务器上失败并出现以下错误:
2018/04/02 15:16:14 [error] 3472#3472: *3 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Call to a member function prepare() on null in /var/www/html/example.com/public_html/includes/pdodatabaseconnect.php:38
Stack trace:
#0 /var/www/html/example.com/public_html/index.php(52): Database->query('SELECT * FROM s...')
#1 {main}
thrown in /var/www/html/example.com/public_html/includes/pdodatabaseconnect.php on line 38" while reading response header from upstream, client: XX.XXX.XXX.XXX, server: example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "example.com"
而完全相同的代码,镜像,在我的本地服务器上运行良好。我看不到PHP 版本中引入的任何PDO 差异会产生这种不同的结果。网上有一些建议可以修改类似的类来解决这个问题,但这似乎是用大锤击中钉子 - 代码在具有镜像文件的不同服务器上以及在运行不同项目但完全相同的数据库类文件的另一台服务器上工作。
当涉及的版本差异很小并且我无法发现任何反映我看到的错误的更改时,我不确定服务器配置可能会对执行代码产生哪些差异。
服务器:
本地(MAMP)
PHP:7.2.1 / MySQL:5.6.38 / nginx:1.13.2
在线(Ubuntu 16.04)
PHP:7.0.28 / MySQL:5.7.21 / nginx:1.10.3
【问题讨论】:
-
您的
$this->dbh为空。配置错误? -
两者的 nginx 配置相同。我不明白为什么它在我的在线服务器上为空,但在我的本地服务器上执行良好。
-
不是 nginx,是 MySql 配置。不同的用户名/密码/主机