-1

I think I got it working again. By forcing the port being used to 80 for apache, and 3306 for mysql. I killed the processes that occupies those port.

This is the exact error showing

<br />
<b>Fatal error</b>: Uncaught mysqli_sql_exception: Access denied for user 'root'@'localhost' (using password: NO) in C:\xampp\htdocs\pcss\public\config\endpoints\db.php:16 Stack trace: #0 C:\xampp\htdocs\pcss\public\config\endpoints\db.php(16): mysqli->__construct('localhost', 'root', Object(SensitiveParameterValue), 'pcss') #1 C:\xampp\htdocs\pcss\public\components\medical-records.php(2): include('C:\\xampp\\htdocs...') #2 C:\xampp\htdocs\pcss\public\index.php(28): include('C:\\xampp\\htdocs...') #3 {main} thrown in C:\xampp\htdocs\pcss\public\config\endpoints\db.php on line 16

and these are the relevant codes

//C:\xampp\htdocs\pcss\public\config\endpoints\db.php
<?php 
    $servername = "localhost";

    switch($_SERVER['HTTP_HOST']){
        //this case part is modified, shouldnt matter i think
        case "my website's link in 000webhost":
        $username = "lorem";
        $password = "lorem";
        $database = "lorem";
        break;

        //this default part is not modified
        default:
        $username = "root";
        $password = "";
        $database = "pcss";
    }
    // Create connection
    $conn = new mysqli($servername, $username, $password, $database);

?>
C:\xampp\htdocs\pcss\public\components\medical-records.php(2)
    include("config/endpoints/db.php");

It used to work just fine until I reformatted my laptop. I even have a website running on 000webhost able to connect w/ MySQL there right now, and all other test files I've done have the same local credentials of

    $servername = "localhost";
    $username = "root";
    $password = "";

and I can login with the shell command 'mysql -u root -p -h localhost' and pressing enter again when prompted for a password,

and I can control the users, databases, and etc... in phpMyAdmin.


Tried these:

"Connect failed: Access denied for user 'root'@'localhost' (using password: YES)" from php function

MySQL said: Documentation #1045 - Access denied for user 'root'@'localhost' (using password: NO)

MySQL Error: : 'Access denied for user 'root'@'localhost'

I've tried the alter thing, basically changes the password of the user you are pointing at. Still don't work. I supposed you should do these stuffs if you can't access phpMyAdmin/MySQL to begin with.

I made a new user account with these credentials just to test:

User name: 'restapi'
Password: 'restapi'

I can login with it in shell's cmd w/ 'mysql -u restapi -p -h localhost', and typing 'restapi' again when prompted for a password. Still can't connect with mysql tho. This is the error:

Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'restapi'@'localhost' (using password: YES) in C:\xampp\htdocs\pcss\public\config\endpoints\db.php:16 Stack trace: #0 C:\xampp\htdocs\pcss\public\config\endpoints\db.php(16): mysqli->__construct('localhost', 'restapi', Object(SensitiveParameterValue), 'pcss') #1 C:\xampp\htdocs\pcss\public\components\medical-records.php(2): include('C:\\xampp\\htdocs...') #2 C:\xampp\htdocs\pcss\public\index.php(28): include('C:\\xampp\\htdocs...') #3 {main} thrown in C:\xampp\htdocs\pcss\public\config\endpoints\db.php on line 16

and this is the code with that new user account:

//C:\xampp\htdocs\pcss\public\config\endpoints\db.php
<?php 
    $servername = "localhost";

    switch($_SERVER['HTTP_HOST']){
        //this case part is modified, shouldnt matter i think
        case "my website's link in 000webhost":
        $username = "lorem";
        $password = "lorem";
        $database = "lorem";
        break;

        //this default part is not modified
        default:
        $username = "restapi";
        $password = "restapi";
        $database = "pcss";
    }
    // Create connection
    $conn = new mysqli($servername, $username, $password, $database);

?>

I also saw this one: Access denied for user 'root@localhost' (using password:NO)

And tried changing the root's password, and I think this is the same thing with the alter thing. Just made me changed my configs (config.inc.php) again to access phpMyAdmin but still couldn't connect using PHP, still throwing the same access denied error.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jason Brody
  • 1
  • 1
  • 4
  • By any chance this has something to do with the ports? I was forced to modify the ports of apache (80 -> 8080) and mysql (3306 -> 3307) since typing localhost wouldn't work w/o those changes. Before the reformatting tho, I didn't have to do anything like this. – Jason Brody Feb 18 '23 at 01:55
  • This is usually a password issue. Reset the password if you need to. Test it on the command line with `mysql -uroot -p[Enter]` after you hit Enter, you'll be prompted for the password. Once you know the password, you can update your code. – user2182349 Feb 18 '23 at 02:31
  • Tried it with >>'mysql -u local -p -h localhost', then pressed enter for password >>'mysql -u restapi -p -h localhost', then 'restapi' for password both worked just fine in the shell cmd, but cant connect in php. – Jason Brody Feb 18 '23 at 02:35
  • I think I got it working again. By forcing the port being used to 80 for apache, and 3306 for mysql. I killed the processes that occupies those port. – Jason Brody Feb 18 '23 at 02:50
  • @JasonBrody You can specify the port number in your `new mysqli()` call, see https://www.php.net/manual/en/mysqli.construct.php#refsect1-mysqli.construct-parameters. So when the MySQL is not running on the default port 3306, provide the port number the `mysqli` class should use. – Progman Feb 18 '23 at 12:27

1 Answers1

-1

Follwing Changes in the file Config.ini.php

$_DVWA[ 'db_server' ]   = '127.0.0.1';

$_DVWA[ 'db_database' ] = 'admin';

$_DVWA[ 'db_user' ]     = 'root';

$_DVWA[ 'db_password' ] = '';

These type of error occur while creating database in the server created with xamp

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175