0

I am new to PHP. I am trying to connect to a MySQL database using mysqli. I created a new user, and it didn't connect and threw the access denied error. I gave database-specific grant in PHPMyAdmin, but still throws error. Then I tried with the root user as well, and it still throws the same error. Here's my code:

<?php

// create mysqli connection
$conn = new mysqli('localhost', 'root', '', 'dbname');

// check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo 'Connected to the database successfully!';

I don't understand why that is happening. Please help me! Thanks in advance...

ADyson
  • 57,178
  • 14
  • 51
  • 63
Darshan B
  • 40
  • 1
  • 6
  • 1
    What is the error message? – Barmar Mar 30 '22 at 15:43
  • We don't know the credentials for your database, sorry. This isn't really a code problem. – ADyson Mar 30 '22 at 15:44
  • @ADyson I am using root user and the password by default for root user is blank. – Darshan B Mar 30 '22 at 15:56
  • @Barmar Error is: Fatal error: Uncaught mysqli_sql_exception: Access denied for user ''@'localhost' (using password: NO) in /path/to/php/file/with/error/ – Darshan B Mar 30 '22 at 15:56
  • That error message indicates that you used an empty username when you called `new mysqli()`. – Barmar Mar 30 '22 at 15:58
  • @Barmar But u can see in the code I have passed root for the username – Darshan B Mar 30 '22 at 16:00
  • 1
    Clearly it's not coming from that code. It doesn't lie. – Barmar Mar 30 '22 at 16:01
  • `the password by default for root user is blank`...maybe by default, but perhaps you or someone else changed it. The database isn't lying to you, it's clearly telling you that you submitted invalid credentials. – ADyson Mar 30 '22 at 16:01
  • And yes `Access denied for user ''@'localhost' ` cannot be from the code shown in the question. It's simply impossible. Double-check what you're telling us and showing us is all from the same place. The error message will include a filename and line number. – ADyson Mar 30 '22 at 16:02
  • @Adyson I got that. There were some permission errors. XAMPP wasn't able to install some dependencies due to some permissions that it required but didn't have. The issue is solved. Thanks! – Darshan B Mar 30 '22 at 16:08

0 Answers0