skip to Main Content

I’m connecting to a MariaDB database using PHP PDO library. When running the code on PhpStorm, everything works fine, but when running on LAMPP I get the following exception:

127.0.0.1:45536 [500]: POST /register - Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /opt/lampp/htdocs/app/DB/DatabaseManager.php:17
Stack trace:
#0 /opt/lampp/htdocs/app/DB/DatabaseManager.php(17): PDO->__construct()
#1 /opt/lampp/htdocs/index.php(31): create_connection_to_db()
#2 [internal function]: {closure}()
#3 /opt/lampp/htdocs/vendor/klein/klein/src/Klein/Klein.php(878): call_user_func()
#4 /opt/lampp/htdocs/vendor/klein/klein/src/Klein/Klein.php(588): KleinKlein->handleRouteCallback()
#5 /opt/lampp/htdocs/index.php(49): KleinKlein->dispatch()
#6 {main}

Next KleinExceptionsUnhandledException: SQLSTATE[HY000] [2002] No such file or directory in /opt/lampp/htdocs/vendor/klein/klein/src/Klein/Klein.php:954
Stack trace:
#0 /opt/lampp/htdocs/vendor/klein/klein/src/Klein/Klein.php(645): KleinKlein->error()
#1 /opt/lampp/htdocs/index.php(49): KleinKlein->dispatch()
#2 {main}
  thrown in /opt/lampp/htdocs/vendor/klein/klein/src/Klein/Klein.php on line 954

The exception is thrown when creating a PDO instance:

DatabaseManager.php


include_once realpath(__DIR__."/../settings.inc.php");

function create_connection_to_db() : PDO {
    global $host, $port, $dbname, $user, $mysql_socket, $password;
    return new PDO("mysql:host={$host};port=$port;unix_socket=$mysql_socket;dbname={$dbname}", $user, $password);
}


settings.php.inc


<?php
$user="root";
$password="";
$dbname="ForoPoly";
$host="127.0.0.1";
$port = "3306";
$mysql_socket="/opt/lampp/var/mysql/mysql.sock";


I’ve tried to specify the MySQL socket, but it not working:

$mysql_socket="/opt/lampp/var/mysql/mysql.sock";

Also, I have enabled pdo_mysql extension and ovweritten socket in laamp’s php.ini:

extension=pdo_mysql
pdo_mysql.default_socket = /opt/lampp/var/mysql/mysql.sock
mysqli.default_socket = /opt/lampp/var/mysql/mysql.sock

It’s important to say that the host is 127.0.0.1, not localhost.

Same thing happens when creating a php test server using the interpreter

2

Answers


  1. Chosen as BEST ANSWER

    Just found the error!,

    it has to do with my file "settings.php.inc". It contains variables for host, port, dbname...

    The thing is that when I was including them, they ended up being empty for some reason.:

    include_once realpath(__DIR__."/../settings.inc.php");
    
    function create_connection_to_db() : PDO {
        global $host, $port, $dbname, $user, $mysql_socket, $password;
        return new PDO("mysql:host={$host};port=$port;unix_socket=$mysql_socket;dbname={$dbname}", $user, $password);
    }
    

    Thank you @ADyson for your help.


  2. You need to decide whether you want to connect via a socket or you want to connect via hostname and port. They are different connection mechanisms, so specifying both – as you’re doing in your code currently – does not make sense.

    Remove one or the other from the connection string and then you should get better results.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search