Hello,

I have just attempted to update my database login page to MySQLi and also use prepared statements but upon submitting a form I receive the error "Access denied for user 'admin'@'localhost' (using password: NO)".

I have been researching this before posting this here but have only come across sites detailing switching server root credentials, etc - I am using a professional hosting site which I pay for (I am not running this on a server on my machine).

I have never received this error message before and I am using the same login credentials as always.

The new code is below.

Any help in the right direction or explanation would be greatly appreciated.

Thank you,
Matthew

`

<?php

$serverName = "localhost";
$userName = "********";
$password = "********";
$dbName = "********";

// Create connection
$conn = new mysqli($serverName, $userName, $password, $dbName);

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

// Prepare and bind
$stmt = $conn->prepare("INSERT INTO Table4 (userName, birthYear, email, password, countries, state, city, zip, company, manager1, manager2) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sisssssisss", $userName, $birthYear, $email, $password, $countries, $state, $city, $zip, $company, $manager1, $manager2 );

    $userName = $_POST['user'];
    $birthYear = $_POST['birthYear'];
    $email = $_POST['email'];
    $password =  $_POST['pass'];
    $countries = $_POST ['countries'];
    $state = $_POST ['state'];
    $city =  $_POST['city'];
    $zip =  $_POST['zip'];
    $company = $_POST ['company'];
    $manager1 = $_POST ['manager1'];
    $manager2 = $_POST ['manager2'];
    $stmt->execute();

    echo "Thank you for registering - New records created successfully!";

    $stmt->close();
    $conn->close();
?>

`

Member Avatar for diafol

Are you absolutely sure that your credentials are correct? This is usually the culprit - a simple oversight. Other issues can be that you have the local site on your machine and you've pointed the DB connection to the remote host. This often fails as the remote host bars remote access unless you allow specific IP access through your cPanel. However, this does not seem to be the case here.
Check your documentation as certain hosts add a prefix for usernames. I was going bananas with one of my uploaded sites until I realised that all usernames had the 'diafolo_' prefix (that won't be your prefix! If you have one at all that is). Are you able to log into phpmyadmin?

diafol:

Hi. I am actually editing the files directly on the hosting site (My machine's server is not even turned on - I rarely use it, only when I am taking online programming classes).

It may very well be an oversite I may with the credentials - I will check that now.

Yes, I am able to login to PHPMyAdmin and am working through it on the hosted site.

I re-checked the login credentials - They appear to be correct.

The thing is, the code is set to throw an error if I am unable to connect to the DB and it does not show an error so I assume I am connecting, but, it then shows the password error.

Member Avatar for diafol

The problem is from the sound of it:

'admin'@'localhost' (using password: NO)"

You're not supplying the password at all.

In the PHP code posted above I have the following:

$password = "********";

The password is being supplied. Nothing in regards to credentials has changed since today when I rebuilt this page for MySQLi and also started to use prepared statements.

Member Avatar for diafol

Well the password is not being passed or you'd get the

using password: YES

response.

Try hard-coding the values thus:

$conn = new mysqli("localhost", "user", "password", "database");

See if it works then

diafol:

I tried as you suggested and received the message:"Connection failed: Access denied for user 'user'@'localhost' (using password: YES)"

Member Avatar for diafol

Ok so, it seems you weren't passing the password after all. I hope you didn't just use the example I gave without changing the "user", "password" and "database" to your own values did you? It seems like you did as your original username was "admin" and now it's "user".

Diafol:

The only area of credentials that I changed was the password to "password". All of the rest is the original code as before.

I do not want and cannot have the password actually be "password" - I need a military-grade password which I plan to implement soon.

Member Avatar for diafol

If you have the following - a common method of setting up a connection ;)

define('DB_HOST', 'localhost');
define('DB_NAME', 'hellcircles');
define('DB_USER', 'diafol');
define('DB_PASS', 'sillybilly');

Then the following should work:

$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

If it fails, try...

$conn = new mysqli('localhost', 'diafol', 'sillybilly', 'hellcircles');

But using your data, obviously.

If it still fails, checck again that the phpmyadmin credentials fit with this. If it works with phpmyadmin but not with your script, I'm pretty much at a loss. Check the prefix thing like I suggested earlier, but am stumped.

You could try setting up a new user (with admin privileges) in phpmyadmin as see if that works in your script. A possible situation would be that you've changed the password at some point (and it's autocompleted in your phpmyadmin login page) and you've forgotten. Sorry, not suggesting you have, but grasping at straws.

This is what I previously always used but did not think it was MySQLi-compliant:

`

    define('DB_HOST', 'localhost');
    define('DB_NAME', 'hellcircles');
    define('DB_USER', 'diafol');
    define('DB_PASS', 'sillybilly');

`

Is it?

UPDATE:

Attempting to submit the form again three times, all data is now saved correctly using the code posted originally, above. But, I receive the error message: "Thank you for registering - New records created successfully!Access denied for user 'admin'@'localhost' (using password: NO)"

How is it possible that the data is being saved when the error message indicates I do not have access?

This is a puzzle that I will have to look into in the morning.

Member Avatar for diafol

Solved? Could you tell us what the issue was? It may help others

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.