Answer the question
In order to leave comments, you need to log in
How to fix the error "Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user ''@'localhost'"?
Hello, I came across such an error, the password, host, user, database name are all correct. The sql query is also correct (tested in the database).
Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)' in D:\Server\OpenServer\domains\e-shopper.ru\app\ components\db.php:8 Stack trace: #0 D:\Server\OpenServer\domains\e-shopper.ru\app\components\db.php(8): PDO->__construct('mysql:host=;dbn ...', NULL, NULL) #1 D:\Server\OpenServer\domains\e-shopper.ru\app\models\Product.php(9): Db::getConnection() #2 D:\Server\ OpenServer\domains\e-shopper.ru\app\controllers\SiteController.php(11): Product::getLatestProducts(6) #3 [internal function]: SiteController->actionIndex() #4 D:\Server\OpenServer\ domains\e-shopper.ru\app\components\route.php(41): call_user_func_array(Array, Array) #5 D:\Server\OpenServer\domains\e-shopper.ru\index.php(6): Router ->run() #6 {main} thrown in D:\Server\OpenServer\domains\e-shopper.ru\app\components\db.php on line 8
$sql = 'SELECT `id`, `name`, price, is_new FROM product '
. 'WHERE status = "1" ORDER BY id DESC '
. 'LIMIT :count';
$result = $db->prepare($sql) or die('Не соединения с бд!');
$result->bindParam(':count', $count, PDO::PARAM_INT);
$result->setFetchMode(PDO::FETCH_ASSOC);
$result->execute();
Answer the question
In order to leave comments, you need to log in
The localhost user does not have access to the database and / or the password is empty.
Check database users and set permissions (phpmyadmin or similar).
check again the login and password to the database, SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)' , the error says that access is denied because no password was entered
Through the console you can in mysql? ( mysql -u root -p
) If you can, then everything is correct with your login / password and you need to look at the database connection configs.
Don't you think that the problem is that the error is ''@'localhost', i.e. The user is simply not logged in. Maybe worth digging in this direction?
Check the port for the database.
I have a different port, 3305, not 3306.
There was a similar error, I decided in a similar way.
If the port is not explicitly specified, then it can simply be assigned to the server address, for example localhost:3305
return array(
'host' => 'localhost',
'dbname' => 'phpshop',
'user' => 'root',
'password' => '',
);
эти данные совпадают с данными в бд
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question