-5

I am getting following error:

"Access denied for user 'root'@'localhost' (using password: NO)?" Unable to authenticate php/Mysql

I am getting this error using lamp server on ubuntu.

<?php
    $not_login = 0;
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $servername = "localhost";
        $username = "root";
        $password = "";
        $con = mysql_connect($servername,$username);
        if ($con) {
            $db = mysql_select_db("ccmsdb",$con);
        }
        else{die(mysql_error());
    }
    $login = "LOGIN";

    $userid=$_POST['userid'];
    $password = $_POST['password'];
    //$password = md5($password);

    if ($db) {
        $query = "SELECT * FROM client WHERE   `userid`='$userid' AND `password`='$password' ";
        $result = mysql_query($query);
        $rows_count = mysql_num_rows($result);

        if ($rows_count>0) {

            header("Location:../welcome.php");
        }

        else
        {
            $not_login=1;
        }
        if(!empty($data))
        {

        }

    }
}
?>

<html>
       <form action=" " method="post">



        <h1>Log-In</h1>

      <p>   <label for="Userid">User Id:</label>

        <input type="text" name="userid">

    </p>

    <p>

        <label for="Password">Password:</label>

        <input type="Password" name="password">

    </p>
    <button>Login</button>

</form>

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • 2
    `mysql_connect($servername,$username);` you also need to pass the `$password` variable in there, even though none may be setup for it. You also have a whole bunch of spaces in `... password` – Funk Forty Niner Dec 30 '15 at 16:22
  • Please refer to the `mysql_connect` [documentation](http://php.net/manual/en/function.mysql-connect.php). This extension is deprecated and has been removed as of PHP7. No new code should be developed that uses on it. – Jeff Lambert Dec 30 '15 at 16:24
  • I added $password but when i clicked Login, nothing happend. – kamran javaid Dec 30 '15 at 16:25
  • also, hate to be that guy, but stop using mysql_...... find a more updated tutorial to follow – I wrestled a bear once. Dec 30 '15 at 16:25
  • ***Step #2 to debugging:*** Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Dec 30 '15 at 16:29
  • and that folks, is why I always post [a "comment"](http://stackoverflow.com/questions/34533174/access-denied-for-user-rootlocalhost-using-password-nounable-to-authent#comment56807766_34533174) before knowing exactly which animal we're dealing with here. V v V v V v V – Funk Forty Niner Dec 30 '15 at 16:34
  • kamranjavaid, it's also a good thing to test a connection with only that in a file. Creating a new file with just the connection and error handling can help you isolate your problem. @Fred-ii-, yep, but that comes with a price. Sometimes people rage that we 'answered' in comments... – FirstOne Dec 30 '15 at 16:40
  • 2
    Plus, your question is a repost of http://stackoverflow.com/q/34526010/ – Funk Forty Niner Dec 30 '15 at 16:40
  • @FirstOne Let them rage; it's but a waste of emotion really and they really should be paying attention, not to mention what century we're in now ;-) – Funk Forty Niner Dec 30 '15 at 16:41
  • why is this thread still going. the relevant question has been answered here half a dozen times. OP has a new issue he should post a new question. – I wrestled a bear once. Dec 30 '15 at 16:45

2 Answers2

-1

You forgot to add password parameter into mysql_connect function, adding it as shown below should resolve your issue.

check this modification:

       <?php
        $not_login = 0;
        if ($_SERVER["REQUEST_METHOD"] == "POST")
          {
        $servername = "localhost";
        $username = "root";
       $password = "";
        $con = mysql_connect($servername,$username, $password);

...

?>
Alpesh Panchal
  • 1,723
  • 12
  • 9