first of all: System Ubuntu 16.04 Apache2 PHP 7.2 DB: MYSQL
now following problem: I have two projects the first project is just a php file with the following code
<?php
$servername ='localhost';
$username = 'root';
$password = '12345';
$dbname = 'test';
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
do stuff ...
}
catch(PDOException $e) {
echo $e->getMessage();
}
This is working fine, I am able to connect to the database and get desired data.
Now I wanted to do it a little bit different:
<?php
class Database {
// DB Params
private $host = 'localhost';
private $db_name = "test";
private $username = "root";
private $password = "12345";
private $conn;
// DB Connect
public function connect() {
$this->conn = null;
echo "connect() - ";
try {
$this->conn = new PDO('myqsl:host=' . $this->host . ';dbname=' . $this->db_name, $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'HEY! Connection Error: ' . $e->getMessage();
}
return $this->conn;
}
}
Now here I get the following error:
could not find driver
I am trying for hours to find a solution but with no success. It works but not in a class ? ....
It was just a typo ... in the class it is
instead of
WARNING! I fall in similar mistake when i try to break connection string in multiples lines and lost 2h also trying reinstall drivers and config:
That throwns an PDOException: could not find driver.
The issue i found with connection string: BREAK LINE + mysql:host... is diferent from mysql:host...
Solution: Just put the ' bellow like that then you not breaking the connection string: