The error could not find driver - PDO Exception in XAMPP
- Open 'php.ini' form xampp control panel.
- You can find location php.ini file using this command php -i | find /i "Configuration File"
- Search or find for php_pdo_mysql.dll
- remove the ';' (semicolon) in the begging of php_pdo_mysql.dll
- save php.ini and restart apache
- 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