-3

im trying to code a simple login in php+mysql. Everytime i hit submit button i get this error

Could not connect to MySQL: Access denied for user 'root'@'localhost' (using password: YES)

This is code that im using to connect to mysql:

<?php
// Create a connection to the logindb database and to MySQL.
// Set the encoding and the access details as constants:
DEFINE ('DB_USER', 'johny');
DEFINE ('DB_PASSWORD', 'some_pass');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'biblioteczka');
// Make the connection:
$dbcon = @mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME)
OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

$ID = $_POST['user'];
$PASSWORD = $_POST['pass'];   
// Set the encoding...
mysqli_set_charset($dbcon, 'utf8');    
function SignIn(){
    session_start();
    if(!empty($_POST['user'])){
        $query = mysql_query("SELECT * FROM userName where user ='$_POST[user]' and pass = '$_POST[pass]'" 
                or die(mysql_error));
        $row = mysql_fetch_array($query) or die(mysql_error());
        if(!empty($row['userName']) AND !empty($row['pass'])){
            $_SESSION['userName'] = $row['pass'];
            echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE...";
        } else {
            echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY...";
        }

    }
}
if(isset($_POST['submit']))
{
    SignIn();
}
?>

I have 2 accounts root and johny, they have all priviliges and i set the passwords and i can work on database that i created.

phpmyadmin config:

phpmyadmin config

I don't know where's the problem at this points that's why im asking for help.

Sorry for my english i'm not a native speaker

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
J. K.
  • 1
  • 3
  • 2
    Your error messages says you are trying to login as root. Your code says you are trying to log in as johny. You've copied something wrong. – Quentin Jun 10 '16 at 16:36
  • 1
    Your code for the question should be **in the question**, not hosted on an external site with a link. – Quentin Jun 10 '16 at 16:36
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) that has been [removed](http://php.net/manual/en/mysql.php) from PHP. You should select a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jun 10 '16 at 16:36
  • 1
    **Danger**: "Not hashing at all" is [an unsuitable hashing algorithm](http://php.net/manual/en/faq.passwords.php); you need to [take better care](https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet) of your users' passwords. – Quentin Jun 10 '16 at 16:37
  • You are attemping to *mix* MySQLi and MySQL functions! You connect via `mysqli_connect` then try to query via `mysql_query`. Since you are *not* connected via the MySQL api, it tries connect on its own. – gen_Eric Jun 10 '16 at 16:39
  • 1
    Just a note: Do not post *screenshots* of code. If you want to include code in the question, copy and paste it in, screenshots of your editor are not helpful. – gen_Eric Jun 10 '16 at 16:39

1 Answers1

2

You connected to the database using the mysqli_ library.

Then you tried to query it with the (obsolete) mysql_ library.

The latter doesn't have a connection to the database, so it tried to open one (and failed).

Pick one library and use it consistently.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335