phpmysqlxampp-vm

How to Solve MySQL Error: Access denied for user root@localhost


i have xampp installed on windows 10 but i updated to windows 11 and i changed port 3306 to 3307 now whene i try to connect to root it send back this error :

Access denied for user 'root@localhost' (using password:NO)

this is my code php

<?php
$host = 'localhost';
$user = 'root';
$pass = ''; // i don't use password
$db = 'projet';

$connect = mysqli_connect($host, $user, $pass, $db);

Solution

  • TL;DR Copy/paste

    <?php
    $connect = mysqli_connect(host: '127.0.0.1', username: 'root', port: 3307, dbname: 'project');
    

    Explanation

    There is a port property in the mysqli Object: https://www.w3schools.com/Php/func_mysqli_connect.asp

    You should pass 3307 to it. Moreover, if your root user doesn't have a password (which is ok for testing but not for production) you shouldn't pass an ampty string. Instead, just leave it empty or null. To achieve this, I'd recommend using named parameters (https://stackoverflow.com/a/19127007/16682019)