0

I've got a login.php file which looks like this:

include "myfuncs.php";

$connect = dbConnection();

$username = $_POST["username"];
$passwort = md5($_POST["password"]);

$query = "SELECT username, password FROM user WHERE username LIKE '$username' LIMIT 1";
$ergebnis = mysql_query($query);
$row = mysql_fetch_object($result);

if($row->password == $passwort)
{
    echo "Hi $username";
    $_SESSION["username"] = $username;

    echo "Login successfully";
}
else
{
    echo "Login doesn't work";
}

and a myfuncs.php file which looks like this:

function dbConnection()
{
$servername = "...";
$username = "...";
$password = "...";
$dbname = "...";

$db_connect = new mysqli($servername, $username, $password, $dbname);

    if ($db_connect->connect_error) 
    {
        die("Connection failed: " . $db_connect->connect_error);
    }
    return $db_connect;
}

Unfortunately the login form doesn't work - it always gives the error "Login doesn't work" even when the username and password matches with the database entry.

12
  • Try to debug your code to find out what is wrong. Commented Oct 15, 2015 at 8:58
  • Are you sure password is stored in DB using md5 ? Commented Oct 15, 2015 at 9:00
  • You are mixing mysql_ and mysqli_ functions. You connect using mysqli, but then run checks using mysql_ functions, which does not know about your mysqli_ connection. Update everything to their mysqli counterparts. Commented Oct 15, 2015 at 9:00
  • @ThinkTank Yes I'm sure, checked it. Commented Oct 15, 2015 at 9:01
  • 1
    @coder No, the other way around. mysql_ functions have been deprecated for a few years now, you should really avoid them as they will be dropped with the upcoming PHP7 release later this year. So use mysqli_ functions for everything. Commented Oct 15, 2015 at 9:04

3 Answers 3

1

Arg, you are mixing a mysqli with class mysql functions. I dont think it works...

It works this way : PHP MySQLI

$stmt = $mysqli->prepare($query)

while ($stmt->fetch()) {
    (...)
}
Sign up to request clarification or add additional context in comments.

Comments

0

I see you have error in your variable name in line #6. try this:

$query = "SELECT username, password FROM user WHERE username LIKE '$username' LIMIT 1";
$result= mysql_query($query);
$row = mysql_fetch_object($result);

Comments

0

There are several problems with your code. In myfuncs.php you use mysqli and after that, in your code you use mysql (without "i"). mysql (without "i") is deprecated, so you should use mysqli everywhere.

More than that, in your code you have:

$query = "SELECT username, password FROM user WHERE username LIKE '$username' LIMIT 1";
$ergebnis = mysql_query($query);
$row = mysql_fetch_object($result);

Please see the bold text from next two lines (it should be the same variable):

$ergebnis = mysql_query($query);

$row = mysql_fetch_object($result);

You should have

$result = mysql_query($query);

if you will use mysql.

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.