0

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:

<?php
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 = <<<EOT
    <form action="register.php" method="POST">
    First Name: <input type="text" name="name" /><br />
    Last Name: <input type="text" name="lname" /><br />
    Username: <input type="text" name="uname" /><br />
    Email: <input type="text" name="email1" /><br />
    Confirm Email: <input type="text" name="email2" /><br />
    Password: <input type="password" name="pass1" /><br />
    Confirm Password: <input type="password" name="pass2" /><br />
    <input type="submit" value="Register" name="submit" />
EOT;
}

?>

and my config.php is:

<?php
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

2
  • 1
    Where is user password ? syntax: mysql_connect($server, $username, $password , $dbname).Are you sure you are apply the empty password in this root user? Commented Mar 30, 2017 at 8:01
  • prasad, I didn't change anything at PhpMyAdmin what is regarding MySQL users so the root user has a default password. If I try to log in with password "root", I get the same result Commented Mar 30, 2017 at 8:06

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, looks like that MySQL default "root" user didn't have an empty or "root" password. I created a new account and then connected. It was school project and they required MySQL and not MySQLi. Thank you!
0

First of all, you'd better use mysqli or pdo (https://stackoverflow.com/a/12860046/2660794)

Once you have modified your code, you need to use the credentials you use to connect to database on your server (eg : the one you use for phpmyadmin).

Comments

0

I'd recommend using mysqli. For your case, the config.php would look like:

<?php 
$mysqli = new mysqli("localhost", "root", "", "MyDB");

if(mysqli_connect_errno()){
    printf("Connection Failed %s\n", mysqli_connect_error());
    exit();
}

session_start();
if(isset($SESSION["REMOTE_ADDR"]) && $SESSION["REMOTE_ADDR"] !=     $SERVER["REMOTE_ADDR"]){
    session_destroy();
    session_start();
}
?>

and instead of "require", you'd do:

include ("config.php");

Hope this helped

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.