7/5/19

How to fix Error could not find driver - PDO EXCEPTION XAMPP

The error could not find driver - PDO Exception in XAMPP
  1. Open 'php.ini' form xampp control panel.
  2. You can find location php.ini file using this command php -i | find /i "Configuration File"
  3. Search or find for php_pdo_mysql.dll
  4. remove the ';' (semicolon) in the begging of php_pdo_mysql.dll
  5. save php.ini and restart apache
  6. As you can see now the error is fixed.
Please should be considered DSN in PDO php
// dsn - data source name $dsn = "mysql:host=".$this->servername.";dbname=".$this->dbname.";charset=".$this->charset;
Database Connection code in PDO:
<?php

class dbh
{
    private $servername;
    private $username;
    private $password;
    private $dbname;
    private $charset;

    public function connect()
    {
        $this->servername = "localhost";
        $this->username = "root";
        $this->password = "";
        $this->dbname = "test";
        $this->charset = "utf8mb4";

        // dsn - data source name
        $dsn = "mysql:host=".$this->servername.";dbname=".$this->dbname.";charset=".$this->charset;


        try {
            // PDO - Represents a connection between PHP and a database server.
            $pdo = new PDO($dsn, $this->username, $this->password);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // If any error occurrs catch function catch the errors.
            return $pdo;
        } catch (PDOException $e) {
            // PDOException
            echo "Connection failed: ".$e->getMessage(). '<br/>Code: '.$e->getCode();
            // Exception::getMessage - gets the Exception message
        }
    }
}

My Answer:

No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *