thecodeholic / php-crash-course-2020

236 stars 228 forks source link

PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /opt/lampp/htdocs/product_crud/03_good/Database.php:12 #8

Open frankndungu opened 2 years ago

frankndungu commented 2 years ago

PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /opt/lampp/htdocs/product_crud/03_good/Database.php:12 Stack trace:

0 /opt/lampp/htdocs/product_crud/03_good/Database.php(12): PDO->__construct()

1 /opt/lampp/htdocs/product_crud/03_good/Router.php(12): app\Database->__construct()

2 /opt/lampp/htdocs/product_crud/03_good/public/index.php(8): app\Router->__construct()

3 {main}

thrown in /opt/lampp/htdocs/product_crud/03_good/Database.php on line 12 [Wed Oct 26 14:05:39 2022] 127.0.0.1:57176 [500]: GET / - Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /opt/lampp/htdocs/product_crud/03_good/Database.php:12 Stack trace:

0 /opt/lampp/htdocs/product_crud/03_good/Database.php(12): PDO->__construct()

1 /opt/lampp/htdocs/product_crud/03_good/Router.php(12): app\Database->__construct()

2 /opt/lampp/htdocs/product_crud/03_good/public/index.php(8): app\Router->__construct()

3 {main}

thrown in /opt/lampp/htdocs/product_crud/03_good/Database.php on line 12 [Wed Oct 26 14:05:39 2022] 127.0.0.1:57176 Closing

frankndungu commented 2 years ago

How I got around this bug; I changed the server port from

$this->pdo = new PDO('mysql:host=localhost;port=3306;dbname=products_crud', 'root', '');

to

$this->pdo = new \PDO('mysql:host=127.0.0.1;port=3306;dbname=products_crud', 'root', '');

This worked well for me.