Работаю с PDO классом , новенький в PHP ..
Получаю ошибку при попытке подключится к базе данных ..
При использовании команды
php -i | grep pdo // Я получаю pdo_mysql.default_socket => /var/mysql/mysql.sock =>
// /var/mysql/mysql.sock
При использовании команды в mySql shell ->
mysql> show variables like '%sock%' ->Я не получаю ни какой информации .
В php.ini файле я просмотрел pdo_mysql.default_socket , которое было в начале пустое после чего я заменил значение на pdo_mysql.default_socket=/var/mysql/mysql.sock
В моем XAMP/xamppfiles/etc/my.cnf в разделе [mysqld] я получаю это информацию :
user = mysql
port = 3306
socket = /Applications/XAMP/xamppfiles/var/mysql/mysql.sock
Нужна помощь , как решить эту проблему с сокетом ? Использую MAC Sierra 10.12.6 .
Не использую Laravel .
Мой код :
Test:
<?php
require_once '../classes/DBConnector.php';
class ConnectionTesting extends TestCase
{
public static function connect($dbdriver,$host,$port,$dbname,$username,$password){
$connection = new DBConnector($dbdriver,$host,$port,$dbname,$username,$password);
print_r($connection->getConnection());
return $connection;
}
}
ConnectionTesting::connect(
'mysql:','127.0.0.1',':3306',
'conlineltd','root','plemia19880@@'
);
The Connection:
<?php
class DBConnector
{
private $user_name;
private $password;
private $db_driver;
private $host;
private $port;
private $db_name;
function __construct($dbdriver,$host,$port,$dbname,$username,$password)
{
$this->checkValidation($dbdriver, $host, $port, $dbname, $username, $password);
$this->host = $host;
$this->user_name = $username;
$this->password = $password;
$this->port = $port;
$this->db_name = $dbname;
$this->db_driver = $dbdriver;
}
private function checkValidation($dbdriver,$host,$port,$dbname,$username,$password)
{
$arr = array(
"name" => $username,
"password" => $password,
"host" => $host,
"port" => $port,
"dbname" => $dbname,
"driver" => $dbdriver
);
foreach ($arr as $key => $value) {
if (empty($value)) {
throw new BadMethodCallException($key . " Can't be empty!");
}
}
}
public function getConnection():PDO{
$connection = null;
try {
$connection = new PDO($this->db_driver . $this->host . $this->port . $this->db_name, $this->user_name, $this->password);
$connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch (PDOException $exception) {
echo "Problems with connection: ".$exception->getMessage();
}
return $connection;
}
}
Error:
Problems with connection: SQLSTATE[HY000] [2002] No such file or directory
Fatal error: Uncaught TypeError: Return value of DBConnector::getConnection() must be an instance of PDO, null returned in /Applications/XAMPP/xamppfiles/htdocs/conlineltd/src/php/classes/DBConnector.php:49 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/conlineltd/src/php/tests/ConnectionTesting.php(9): DBConnector->getConnection() #1 /Applications/XAMPP/xamppfiles/htdocs/conlineltd/src/php/tests/ConnectionTesting.php(24): ConnectionTesting::connect('mysql:', '127.0.0.1', ':3306', 'conlineltd', 'root', 'plemia19880@@') #2 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/conlineltd/src/php/classes/DBConnector.php on line 49