Wednesday, 16 August 2017

PHP MySQL - Access denied for user



I know that there are a tons of related threads but none of them fixes my problem. i'm having a problem connecting to my database. I get the following error:
Access denied for user ''@'localhost' (using password: NO)



My code for register.php is the following:



require 'config.php';


if(isset($_POST['submit'])){
//does verification
$mail1 = $_POST['email1'];
$mail2 = $_POST['email2'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];

if(($mail1 == $mail2) && ($pass1 == $pass2)){
//everything is k
$name = mysql_escape_string($_POST['name']);

$lname = mysql_escape_string($_POST['lname']);
$uname = mysql_escape_string($_POST['uname']);
$email1 = mysql_escape_string($_POST['email1']);
$pass1 = mysql_escape_string($_POST['pass1']);

$pass1 = md5($pass1);

//Checks if username is taken
$check = mysql_query("SELECT * FROM users WHERE uname = '$uname'")or die(mysql_error());
if (mysql_num_rows($check)>=1) {

echo "Username already taken";
} else {
mysql_query("INSERT INTO `users` (`first_name`, `last_name`, `username`, `mail`, `password`, `id`) VALUES ('$name', '$lname', '$uname', '$email1', '$pass1', NULL)") or die(mysql_error());
echo "Registration Successful";
} }
else {
echo "sorry, something doesn't match.";
}
} else {
//displays form

echo $form = <<

First Name:
Last Name:
Username:
Email:
Confirm Email:
Password:
Confirm Password:


EOT;
}

?>


and my config.php is:



mysql_connect("localhost", "root", "");

mysql_select_db("MyDB");
?>


What to do? My PHP version is 5.6.27 and MySQL version is 5.6.33


Answer



Aside from the fact that you should be using mysqli functions instead of mysql for security reasons, have you checked:




  • Your MySQL username is correct?


  • That a password is not required?

  • The database you are connecting to is correct?



The do:



$db = mysql_connect("localhost", "root", "");
mysql_select_db("MyDB", $db);



And your queries should be:



$query = mysql_query("SELECT * FROM users WHERE uname = '$uname'", $db);

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...