mySQL Access Denied Error After New User Created - php

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 () );
}

Related

PhpMyAdmin connection

I am trying to connect my Database on PhpMyAdmin with my webPage. However I keep getting this error:
Connection failed: Access denied for user 'root'#'localhost' (using password: NO)
My code (php) is:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_client2";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
I do not understand what is wrong:
[enter image description here][1]
[1]: https://i.stack.imgur.com/vYBYy.png
Hope you can help me, thank you in advance.
Please enter a password to the phpcode $password = "";
phpmyadmin disabled root login by default.
If you want to allow root login you will have to update /etc/phpmyadmin/config.inc.php and set
$cfg['Servers'][$i]['AllowRoot'] = TRUE;
Also mysql change the ability to login from root out of the shell.
please see this link https://askubuntu.com/questions/763336/cannot-enter-phpmyadmin-as-root-mysql-5-7
The recommendation is to create an account other than root and grant all privileges.

Connection error MySQL

I copied this from W3Schools and when I try to run it it gives me the a error. I am really new to MySQL so I am trying to fix this but I don't know how.
ERROR:
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user
'username'#'localhost' (using password: YES) in
C:\xampp\htdocs\Informatica\test.php on line 10 Connection failed:
Access denied for user 'username'#'localhost' (using password: YES)
<html>
<body>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
</body>
</html>
your username or password is incorrect if you are very new and just starting with the local server try
$username = "root";
$password = "";
as your username and password
W3schools don't know your localhost details, do the following
$servername = "localhost";
$username = "root"; //put your username here
$password = ""; // your password here
Leave password empty and put "root" in username, that's the default xampp setting.
you have to add correct information provided by your hosting provider here :
$servername = "localhost";
$username = "username";
$password = "password";
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
//Connection to mysql check
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysqli_select_db($conn,'databaseName');
?>

Connection failed: Access denied for user ''username'#'localhost' (using password: YES)

I am trying to connect to MySQL on the server but gives following error. User is created and granted all privileges.
It's working on local machine but not after deploying to a server.
Also mysql_connect function works but new mysqli() gives access denied as mentioned below.
Also Tried adding port.
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user ''usernam'#'localhost' (using password: YES) in /home/domainname/public_html/autopublish/test.php on line 8
Connection failed: Access denied for user ''username'#'localhost' (using password: YES)
<?php
$servername = "localhost";
$username = "'xxxxx";
$password = "xxxxx";
$dbname = "xxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password); **//line 8**
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "no connection";
}
$sql = "SELECT * FROM pages";
$result = $conn->query($sql);
$data = array();
$arr = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
}
} else {
echo "No Token";
}
print_r(json_encode($data));
$conn->close();
?>
Either the user does not have the required rights for the database or the password is wrong.
Otherwise remove the user and use the following statement to create:
1.CREATE USER 'user'#'localhost' IDENTIFIED BY '123456789';
2.
3.GRANT ALL PRIVILEGES ON project.* TO 'user'#'localhost' WITH GRANT OPTION;
or try this:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
//Create
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);
echo "Yaaay";

php/mysql connection error(with config file)

I am developing a login/register system for a website but when i'm trying to connect to the database it gives this error:
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'#'localhost' (using password: YES) in C:\xampp\htdocs\register.php on line 17
I'm using a external config file wich is been imported.
My config file:
// Connection details
$servername = "127.0.0.1";
$username = "root";
$password = "password";
$dbname = "database";
My connection code:
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
I found one solution that worked to connect but then my config file was useless.
No config but only this:
// Create connection
$conn = mysqli_connect('127.0.0.1', 'root', 'password', 'database');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
If someone nows a solution where i can keep using my config file please post it here.
Thanks in advance.
Well, if the connection works with this values:
$conn = mysqli_connect('127.0.0.1', 'root', 'password', 'database');
But not with this values:
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "cloud";
Why don't you just set the "working" values into your config file. So it would look like this:
$servername = "127.0.0.1";
$username = "root";
$password = "password";
$dbname = "database";
Another solution which is described here includes the creation of a new user, so you don't connect via root, but the new created user.

Access denied for user 'db_name'#'localhost' (using password: NO)

I have created a site that is working fine on localhost, but when i uploaded it on server its giving me the following error message.
Access denied for user 'azher94_ivsapps'#'localhost' (using password: YES)
There are my setting for database connection
$database= "db_name";
$server = "";
$username= "db_username";
$password= "db_password";
if(mysql_connect($server, $username, $password) && mysql_select_db($database))
//if( mysqli_connect($server, $username, $password , $database) )
{
echo "connected";
}
else
{
echo mysql_error();
}
Kindly guide me what i am doing wrong here.
Thanks
For password you are using $password and after that you are using $Password with capital P. Please change it to $password.
First of all you need to define the localhost and one more thing you are doing wrong is you are not calling the variable properly. Use the code below
$database= "db_name";
$server = "localhost";
$username= "db_username";
$password= "db_password";
if(mysql_connect($server, $username, $password,$database) )
//if( mysqli_connect($server, $username, $password , $database) )
{
echo "connected";
}
else
{
echo mysql_error();
}
Hope this helps you
Try with this code it will work
$con=mysqli_connect("localhost","root","your password");
mysqli_select_db("your_db",$con);

Categories