BBallMick BBallMick - 6 months ago 8
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:

<?php
error_reporting(E_ALL);
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 = "";

if($_SERVER["REQUEST_METHOD"] == "POST"){

if(empty($_POST["username"])){
$errUsername = "Username is required";
}
else{
$username1 = prepareInput($_POST["username"]);

}

if(empty($_POST["password"])){
$errPassword = "Password is required";
}
else{
$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);
$result->close();
$conn->close();
}

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

Answer

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.

<?php
    /* $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";
?>