0

I have this function :

function user_exists($username) {
    $username = sanitize($username);
    $query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"); //or die(mysql_error());
    return (mysql_result($query, 0) == 1) ? true : false;
}

for checking user is exist or not ..

for this forum :

    <?php

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

        if (empty($username) === true or empty($password) === true) {
            $errors[] = 'You need to enter username and password';
        } else if (user_exists($username) === false) {
            $errors[] = 'We can\'t find that username. Have you '.'<a href="registeration.php">registered</a>'.'?';
        } else if (user_active($username) === false) {
          $errors[] = 'You haven\'t activated your account yet !';
        } else {

            if (strlen($password) > 32) {
                $errors[] = 'Password is too long';

            }

            $login = login($username, $password);
            if ($login === false) {
               $errors[] = 'The username / password compination is incorrect';
            }else {
                $_SESSION['user_id'] = $login;
                header('location: index.php');


            }
        }


    } else {

        $errors[] = 'No data recieved';

    }
    include_once "panels/header.php";
    if (empty($errors) === false) {
    ?>

        <div class="row">
            <div class="container">
                <div class="col-xs-6 col-sm-3 col-md-9">
                    <b>We tried to log you in, but ...</b><br />
                    <?php
                     echo output_errors($errors);
                    }?>

But it gives me this error :

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Knowtodo\functions\users.php on line 45

and show that the user is not found :

We tried to log you in, but ... We can't find that username. Have you registered?

Can you help me please ..

Thanks

Shifter
  • 9
  • 3
  • Can you tell me why are you using/fetching COUNT? –  Feb 24 '15 at 11:04
  • to check if user exist or not – Shifter Feb 24 '15 at 11:12
  • you can use simple select * from query and then check using mysql_num_rows() function. –  Feb 24 '15 at 11:13
  • function user_exists($username) { $username = sanitize($username); $query = mysql_query("SELECT * FROM users WHERE username = '$username'"); //or die(mysql_error()); $num = mysql_num_rows($query); return (mysql_result($num, 0) == 1) ? true : false; } Shows me the same problem – Shifter Feb 24 '15 at 11:19
  • You are using this line for? return (mysql_result($num, 0) == 1) ? true : false; –  Feb 24 '15 at 11:22
  • ok what it should be i'm not professional – Shifter Feb 24 '15 at 11:23
  • you can replace that line with if($num == 0){ return 0; } else{ return 1; } –  Feb 24 '15 at 11:24
  • Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Knowtodo\functions\users.php on line 45 – Shifter Feb 24 '15 at 11:27
  • you can try echo your query and see what you are getting in $username variable –  Feb 24 '15 at 11:31
  • echo "SELECT * FROM users WHERE username={$username}"; and see what prints there. –  Feb 24 '15 at 11:32
  • my user name called shifter SELECT * FROM users WHERE username=Shifter Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Knowtodo\functions\users.php on line 45 – Shifter Feb 24 '15 at 11:35
  • Your query:- "SELECT * FROM users WHERE username='{$username}'"; I have added single quote before and after {$username} –  Feb 24 '15 at 11:40
  • i can't understand you here can you give me your email ?! – Shifter Feb 24 '15 at 13:45

0 Answers0