BBallMick BBallMick - 1 year ago 39
MySQL Question

Why am I getting an access denied error when connecting to MySQL db from php page

EDIT: I found the problem. The connection problem was located on the other page I was redirecting to, which had the wrong credentials, I'm an idiot.

This is my first time asking a quesiton in here, so bear with me.

I wanted to test to see whether or not I am able to insert data into my MySQL database through my .php page. Although I seemingly can't connect to my database, even though the username, password and so on are all correct. I use the same credentials, when I log on to the local instance trough MySQL Workbench.

The error i get in my browser says this:

Connection failed: Access denied for user 'root'@'localhost' (using password: YES)

Also this is my first time coding php, so the code is probably littered with errors, feel free to point them out.

Here's my code:

ini_set('display_errors', '1');

function prepareInput($input){
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;

$servername = "localhost";
$username = "root";
$password = "1234";
$dbname = "praktikdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

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


$username1 = $password1 = "";
$errUsername = $errPassword = "";


$errUsername = "Username is required";
$username1 = prepareInput($_POST["username"]);


$errPassword = "Password is required";
$password1 = prepareInput($_POST["password"]);
$sql = "INSERT INTO users (username, password) VALUES('$username1', '$password1')";
$result = $conn->query($sql);
if(!$result) die("Something went wrong". $conn->error);

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
<form action="login.php" method="post">
Username: <input type="text" name="username">
<span class="error"> <?php echo $errUsername?></span>
Password: <input type="text" name="password">
<span class="error"> <?php echo $errPassword?></span>
<input type="submit" name="btnRegister" name="Register"/>

Answer Source

Your code is giving you a credentials problem (detected by your "Check connection" piece of your code), and that is derived of the users configuration in your MySQL Workbench. You need to configure the user you will use for your PHP connection to MySQL, along with the password, and the server the user connecting will be limited to (in this case is the localhost), and the privileges your user will have.

Also, and this is just for connecting to your database (for now consider that you won't execute a SQL query in your code), you can check if the connection it's ok with just your username, the password and the server name.

    /* $servername, $username and $password goes here */ 

    $conn = new mysqli($servername, $username, $password);

    if($conn->connect_error) {
         die("Connection failed:" $conn->connect_error);

    echo "Connection successful";