I made some projects with using mysql and there was no problem. Today my friend show me how to create user in mysql by :
Create database cc;
use cc
CREATE USER 'testing'@'localhost' IDENTIFIED BY 'pass';
GRANT ALL ON cc.* TO 'testing'@'localhost';
Now I tried to use my project but I got following error :
Connection failed: Access denied for user ''@'localhost' to database 'login'
I deleted previously created user :
drop user 'testing'@'localhost';
I managed to delete that user, I checked user with :
select User from mysql.user;
testing account was gone but now I have 3 root accounts, I didn't understand why. Since I deleted that "testing" account, I tried my project again and I got the same error. Why I can't use default 'root'
account anymore?
EDIT :
Error part of php code :
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "login";
// Error is in following line
$conn = mysqli_connect ( $servername, $username, $password, $dbname );
I also tried this :
GRANT ALL ON login.* TO 'root'@'localhost';
show grants;
Result is :
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION
GRANT ALL PRIVILEGES ON login.* TO 'root'@'localhost'
It seems like 'root' has access to everything. Why am I getting error?
EDIT 2:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "login";
$username = $_POST['username'];
$pass = $_POST['password'];
// Create connection
$conn = mysqli_connect ( $servername, $username, $password, $dbname );
// Check connection
if (! $conn) {
die ( "Connection failed: " . mysqli_connect_error () );
}
Your over riding the previously set $username variable with the $_POST['username'] data which appears to be empty. So mysqli_connect attempts to connect to the database with a blank username.
To correct this, rename the $username variable to something else which doesn't conflict.
Example:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "login";
$username_post = $_POST['username'];
$pass = $_POST['password'];
// Create connection
$conn = mysqli_connect ( $servername, $username, $password, $dbname );
// Check connection
if (! $conn) {
die ( "Connection failed: " . mysqli_connect_error () );
}