1

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?

DevOpsSauce
  • 1,319
  • 1
  • 20
  • 52
  • 2
    `PDO` lives in the gobal namespace either use `new \PDO` or `use PDO;` – DarkBee Jan 14 '19 at 05:28
  • https://stackoverflow.com/questions/11813381/php-fatal-error-class-pdo-not-found –  Jan 14 '19 at 05:37
  • @DarkBee the error message would be different if a namespace was involved – Phil Jan 14 '19 at 05:38
  • They're running PHP 5.3 in that question. Does that make a difference for me? – DevOpsSauce Jan 14 '19 at 05:38
  • 2
    Your web-based and CLI PHP probably use different config files. Try adding an `info.php` file with ` – Phil Jan 14 '19 at 05:39
  • I did that. Forgot to mention it. I'll add it in my question. – DevOpsSauce Jan 14 '19 at 05:41
  • Why does only your `pdo.so` extension end in `.so`? Have you enabled [`display_startup_errors`](http://php.net/manual/en/errorfunc.configuration.php#ini.display-startup-errors)? – Phil Jan 14 '19 at 05:42
  • Lemme check, and if I haven't, I will enable them right quick. – DevOpsSauce Jan 14 '19 at 05:42
  • I trust you can see the difference between `/etc/php/7.2/apache2/php.ini` (your config file) and `/etc/php/7.0/apache2` (as reported by `phpinfo()`). – Phil Jan 14 '19 at 05:45
  • Wait.....why the heck is it showing php/7.0???? – DevOpsSauce Jan 14 '19 at 05:52
  • *"I moved to a new hard drive, and copied everything necessary to the best of my knowledge."* - Installations might install files that the OS needs to reference or `.ini` files. I can't say for certain but it does make me raise a brow. This might also be a permissions issue. – Funk Forty Niner Jan 14 '19 at 05:52
  • I copied Apache configs and my MySQL database from /var/lib/mysql. That stuff is working. PHP, Apache, and MySQL were all installed from scratch, only configs for Apache and a single database and its tables were kept. – DevOpsSauce Jan 14 '19 at 05:53
  • Does the output from `phpinfo()` (in the browser) now show the PDO and PDO_MySQL extensions enabled? How about those missing `.so` extensions? Did you end up trying `display_startup_errors`? – Phil Jan 14 '19 at 06:10
  • Typically for extensions / modules, you would just enable them in the relevant config file in `/etc/php/7.2/apache2/conf.d`, not the main `php.ini` – Phil Jan 14 '19 at 06:12

1 Answers1

0

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.

DevOpsSauce
  • 1,319
  • 1
  • 20
  • 52