0

I have this error "Warning: mysql_result(): user_id not found in MySQL result index 13 in C:\xampp\htdocs\core\functions\users.php on line 14"

I think i don't know how to read from my Database, it looks like

this

Here is my users code

<?php
    function user_exists($username){
            $username = sanitize($username);
            return (mysql_result(mysql_query("SELECT COUNT(`uID`) FROM `user` WHERE `uUserName` = '$username'"), 0) == 1) ? true : false;
    }

    function user_active($username){
            $username = sanitize($username);
            return (mysql_result(mysql_query("SELECT COUNT(`uID`) FROM `user` WHERE `uUserName` = '$username' AND `uActive` = 1"), 0) == 1) ? true : false;
    }

    function user_id_from_username($username){
            $username = sanitize($username);
            return mysql_result(mysql_query("SELECT `uID` FROM `user` WHERE `uUserName` = '$username'"), 0, 'user_id');
    }

    function login($username,$password){
            $user_id = user_id_from_username($username);

            $username = sanitize($username);
            $password = md5($password);

            return (mysql_result(mysql_query("SELECT COUNT(`uID`) FROM `user` WHERE `uUserName` = '$username' AND `uPassword` = '$password'"), 0) == 1) ? $user_id : false;
    }

?>

This is my login system code

if(empty($_POST) === false){
        $username = $_POST['username'];
        $password = $_POST['password'];

        if(empty($username) === true || empty($password) === true){
                $errors[] = 'You need to enter a Username and Password';
        } else if(user_exists($username) == false){
                $errors[] = 'We cannot find that Username, have you registered?';

        } else if(user_active($username) === false){
                $errors[] = 'You have not activated your account.';
        } 
        else {
                $login = login($username,$password);
                if($login === false){
                        $errors[] = 'The username or password is incorrect';
                } else {
                        echo 'ok';
                }
        }

        print_r($errors);

}
Paul Williams
  • 1,554
  • 7
  • 40
  • 75
  • Database queries are generally case-insensitive. Your comparison of the password as plain text at the DB level is a large red flag that your app will be very vulnerable to various hacks. -- edit: I see the hash now, sorry, ignore me – Nikki9696 Nov 13 '15 at 19:58
  • The problem lies on this line: `mysql_result(mysql_query("SELECT `uID` FROM `user` WHERE `uUserName` = '$username'"), 0, 'user_id');` You're using 'user_id' as the offset and not a number or field that mysql recognizes or sees. Try changing it to `uID` instead. – Paul Williams Nov 13 '15 at 19:58
  • I know I am trying to easily distinguish between things right now. So I make it obvious. But it still cant find the password, any help with that? – Azmi Elqutob Nov 13 '15 at 20:07

2 Answers2

1

You have an error at line 13 where you specify 'user_id' as an offset.

You can simply get the uID column and store the value if that row exists.

$result = mysql_query("SELECT `uID` FROM `user` WHERE `uUserName` = '$username'");
$user_id = 0;   // default value, meaning user not found

if ($result && mysql_num_rows($result) > 0){
    $row = mysql_fetch_assoc($result);
    $user_id = $row[0];
}

return $user_id;

Then you can modify your login function to check whether user_id > 0 or not. If it's larger than 0, then you got that user's id.

berkayk
  • 2,376
  • 3
  • 21
  • 26
0

Currently the only problem that I see is your user_id_from_username function.

You're trying to set an offset to a field that doesn't exist and mysql doesn't find it. So it's throwing an error:

function user_id_from_username($username){
        $username = sanitize($username);
        return mysql_result(mysql_query("SELECT `uID` FROM `user` WHERE `uUserName` = '$username'"), 0, 'uID');
}

Try the above or leave off the uID since it's not a mandatory but rather an optional parameter.

Insert obligatory, you should be using mysqli instead of mysql at this point if your PHP version supports it.

Paul Williams
  • 1,554
  • 7
  • 40
  • 75