PHPWarningDatabase ErrorApril 30, 2026

Database Error

PDOStatement::execute(): SQLSTATE[HY000] [2002] No such file or directory in /var/www/html/database.php on line 17

What This Error Means

This error occurs when the PDO database connection fails to connect to the database due to a missing or incorrect file path to the database socket file.

Why It Happens

The error happens because the $pdo->dbname or $pdo->host is set to a non-existent file path, or the file path is incorrect due to a typo or mismatch in the operating system's file path conventions.

How to Fix It

  1. 1To fix this error, ensure that the PDO database connection parameters are correctly set. Verify that the $pdo->dbname and $pdo->host are set to the actual path to the database socket file, and that the path is correct for your operating system. For example, on Unix-based systems, the path should be /var/run/mysqld/mysqld.sock, while on Windows, it should be C:\windows\mysql\mysql.sock. You can also try setting the PDO::ATTR_EMULATE_PREPARES to true to force PDO to emulate the prepared statements, which can help with connection issues.

Example Code Solution

❌ Before (problematic code)
PHP
$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'password');
✅ After (fixed code)
PHP
$pdo = new PDO('mysql:host=localhost;dbname=mydb;unix_socket=/var/run/mysqld/mysqld.sock', 'root', 'password');

Fix for PDOStatement::execute(): SQLSTATE[HY000] [2002] No such file or directory in /var/www/html/database.php on line 17

Related PHP Errors

Related PHP Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error