Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Notice : Undefined variable: user in C:\\xampp\\htdocs\\php\\connecting.php Fata

ID: 659185 • Letter: N

Question

Notice: Undefined variable: user in C: mpphtdocsphpconnecting.php

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'database'' in C: mpphtdocsphpconnecting.php:51 Stack trace: #0 C: mpphtdocsphpconnecting.php(51): PDO->__construct('mysql:host=loca...', NULL, '') #1 {main} thrown inC: mpphtdocsphpconnecting.php

i used the following code

<?php
              
               $dbname = "root";
               $pass = "";
               $dbname = "database";
               $host = "localhost";
              
$dbHandle = new PDO('mysql:host='.$host.';dbname='.$dbname, $user, $pass);

?>

how can i fix this error and connect to my database

Explanation / Answer

two cases are possible

1)remove single quotes around dbname as in

$dbHandle = new PDO("mysql:host=localhost;dbname=xxxxxx", 'admin', 'xxxxxxx');

2)Have you created an account for admin@localhost in mysql? PDO's connecting just fine, but you're using incorrect credentials to log into the database...see the mysql docs..

http://dev.mysql.com/doc/refman/5.5/en/adding-users.html

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote