douxuanou2787
douxuanou2787
2019-01-14 05:26

即使在Web服务器上启用PDO,PDO也会失败

已采纳

I moved to a new hard drive, and copied everything necessary to the best of my knowledge. I try to load any page that requires my PDO Database object and it crashes.

This is the error:

Fatal error: Uncaught Error: Class 'PDO' not found in /var/www/the_website.net/config/DB.php:20 Stack trace:
#0 /var/www/the_website.net/public_html/index.php(16): DB->__construct()
#1 {main} thrown in /var/www/the_website.net/config/DB.php on line 20

This is the line (and few surrounding lines) in question in DB.php:

public function __construct($username='username', $password='supersecretpassword', $host='localhost', $dbname='database', $options=[])
{
    $this->isConnected = true;
    try {
        $this->db_data = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8",$username, $password, $options);
        $this->db_data->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->db_data->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
        throw new Exception($e->getMessage());
    }
}

It's just the creation of the object.

Here are my enabled modules in /etc/php/7.2/apache2/php.ini (I just did them all because nothing was working anyway):

extension=openssl
extension=pdo_firebird
extension=pdo.so
extension=pdo_mysql
extension=pdo_oci
extension=pdo_odbc
extension=pdo_pgsql
extension=pdo_sqlite

Here is the result of php -m

root@HAL4:/etc/php/7.2/apache2# php -m
[PHP Modules]
calendar
Core
ctype
date
exif
fileinfo
filter
ftp
gettext
hash
iconv
json
libxml
mysqli
mysqlnd
openssl
pcntl
pcre
PDO <-- Still doesn't work
pdo_mysql <-- Nope. Still not working.
Phar
posix
readline
Reflection
session
shmop
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
Zend OPcache
zlib

[Zend Modules]
Zend OPcache

I have run phpenmod for both modules.

My former hard drive was running php 7.0. Now I'm running php 7.2.

Running <?php phpinfo(); ?> in a test page tells me:

Configuration File (php.ini) Path   /etc/php/7.0/apache2
Loaded Configuration File   (none)
Scan this dir for additional .ini files /etc/php/7.0/apache2/conf.d
Additional .ini files parsed    (none)

Update I removed the php7.0 modules completely from /usr/lib/apache2/ so now everything is php7.2. My phpinfo() file displays the correct paths:

Configuration File (php.ini) Path   /etc/php/7.2/apache2
Loaded Configuration File   /etc/php/7.2/apache2/php.ini
Scan this dir for additional .ini files /etc/php/7.2/apache2/conf.d

Still getting the Class'PDO' not found error, however.

What else can I check? Why are they not loading?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dstnlhhv791576 dstnlhhv791576 2年前

    I purged PHP, and started over. I did the exact same thing I did before and followed the errors. I kept getting the same fatal error, until I did:

    apt install php7.2-mysql
    

    My problem was resolved.

    点赞 评论 复制链接分享