R
R
Roman Romanov2016-01-11 09:35:07
PHP
Roman Romanov, 2016-01-11 09:35:07

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();

Thanks in advance for your reply

Answer the question

In order to leave comments, you need to log in

7 answer(s)
X
xmoonlight, 2016-01-11
@Roman_Romanov

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).

S
Sergey Zelensky, 2016-01-11
@SergeyZelensky-Rostov

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

S
seriogja, 2016-01-11
@seriogja

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.

A
Anton Izmailov, 2016-01-11
@WapGeaR

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?

S
StasEx, 2020-05-09
@StasEx

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

R
Roman Romanov, 2016-01-11
@Roman_Romanov

return array(
    'host' => 'localhost',
    'dbname' => 'phpshop',
    'user' => 'root',
    'password' => '',
);
эти данные совпадают с данными в бд

L
Leagnus, 2017-02-16
@Leagnus

If on localhost, then check among the processes whether mysqld or Mariadb service is running from another stack.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question