Alex Knopp
Alex Knopp

Reputation: 51

PHP PDO - could not find driver but driver is installed and enabled

I have PHP7.2 (apache) running on my local Ubuntu 16 machine. All my applications were working until yesterday when I tried to connect to my GSQL database. I had already been successfully connecting and sending INSERT statements all day until I got the error "could not find driver". So something seems to have changed.

Here is my phpinfo():

Screenshot from my phpinfo()

Here is the code Im using to connect. The only thing that has changed is that Im now trying to connect with the SSL options. I have the certificate files stored locally in a directory above my doc root. I tried both with and without the SSL options and I still get the same error.

//Connect to the DB
$this->connection = new PDO("
    mysql:host=$this->servername;
    dbname=$this->database",$this->u,$this->p,[
        PDO::MYSQL_ATTR_SSL_KEY    => $this->ck,
        PDO::MYSQL_ATTR_SSL_CERT   => $this->cc,
        PDO::MYSQL_ATTR_SSL_CA     => $this->sc,
        PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
    ]
);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Ive tried all sorts of different solutions from the google and stack search results to no avail.

Upvotes: 1

Views: 291

Answers (1)

Alex Knopp
Alex Knopp

Reputation: 51

Big thanks to @KIKO Software for helping me with this and pointing me at my error. Can't thank you enough.

The error was breaking the DSN onto multiple lines.

//Connect to the DB
$this->connection = new PDO("
  mysql:host=$this->servername;dbname=$this->database",$this->u,$this->p,[
    PDO::MYSQL_ATTR_SSL_KEY    => $this->ck,
    PDO::MYSQL_ATTR_SSL_CERT   => $this->cc,
    PDO::MYSQL_ATTR_SSL_CA     => $this->sc,
    PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
  ]  
);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Upvotes: 1

Related Questions