Back to Writing

FuelPHP Undefined class constant MYSQL_ATTR_COMPRESS

Fixing the 'Undefined class constant MYSQL_ATTR_COMPRESS' error in FuelPHP.

Eli Geske 2 min read
php fuelphp mysql pdo

If you are working with the FuelPHP framework and suddenly encounter the error Undefined class constant 'MYSQL_ATTR_COMPRESS', it’s likely due to a missing or improperly configured PDO MySQL driver on your server.

This error usually occurs when FuelPHP attempts to establish a database connection using PDO but cannot find the specific MySQL attribute constant.

The Cause

The constant PDO::MYSQL_ATTR_COMPRESS is specific to the MySQL driver for PDO. If your PHP installation has PDO enabled but the MySQL-specific extension is missing or not compiled with the correct client libraries, this constant will be undefined.

The Fix

1. Check your PHP Extensions

Ensure that php_pdo_mysql is enabled in your php.ini file. Look for a line like:

extension=pdo_mysql.so ; (Linux)
extension=php_pdo_mysql.dll ; (Windows)

Make sure it’s not commented out.

2. Install Missing Libraries

On Ubuntu/Debian systems, you might need to install the package explicitly:

sudo apt-get install php5-mysql

(Or php7.x-mysql depending on your version).

3. FuelPHP Config Workaround

If you cannot change the server configuration (e.g., on shared hosting) and you don’t actually need connection compression, you can check your db.php configuration in FuelPHP.

Ensure that you haven’t explicitly set a compression attribute in your connection options if the driver doesn’t support it.

// app/config/db.php
return array(
    'active' => 'default',
    'default' => array(
        'type'        => 'pdo',
        'connection'  => array(
            'dsn'        => 'mysql:host=localhost;dbname=my_db',
            'username'   => 'user',
            'password'   => 'pass',
            'persistent' => false,
            'compress'   => false, // Set this to false
        ),
        ...
    ),
);

Legacy Comments Archive

G
Guest
October 15, 2012 at 2:20 am

Installing the php5-mysql package fixed it for me on my Vagrant box. Thanks!

M
Mike
November 1, 2012 at 9:45 pm

I had this issue on a WAMP setup. Had to enable the extension in the WAMP menu.