Jump to content

error when trying to login.


cardwell164

Recommended Posts

I am following this oop registration and login tutorial using PDO. It seems easy to understand that's why i am following it but i am currently having an issue when logging in. I don't have issues when registering as it pushes the data into the database but when i try to login its giving me this error "SQLSTATE[42000]: Syntax error or access violation: 1630 FUNCTION lar.COUNT does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual". "lar" is the name of my database. i double checked the spelling in my code to check if its a typo error but there is nothing wrong that i can find. i am not sure if the problem is with the syntax of my sql query or if theres a problem with the function i created to check for the query.

 

here is the code for my login.php (i did not include the html code in here to save space)

<?php

require 'core/init.php';
$general->logged_in_protect();

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

    if(empty($username) === true || empty($password) === true)
    {
        $errors[] = 'Sorry, but we need your username and password.';
    }
    else if($users->user_exists($username) === false)
    {
        $errors[] = 'Sorry that username does not exist';
    }
    else if($users->email_confirmed($username) === false)
    {
        $errors[] = 'Sorry, but you need to activate your account. Please check your email.';
    }
    else
    {
        $login = $users->login($username, $password);
        if($login === false)
        {
            $errors[] = 'Sorry, that username/password is invalid';
        }
        else
        {
            session_regenerate_id(true); //destroying the old session and creating a new one
            $_SESSION['id'] = $login;
            header('Location: home.php');
            exit();
        }
    }
}
?>

and this is my code to my users class where i suspect there is a problem. users.php (i only included the user_exists and login function since all the rest seems to work fine)

class Users
{
    private $_db;

    public function __construct($database)
    {
        $this->_db = $database;
    }

    public function user_exists($username)
    {
    $query = $this->_db->prepare("SELECT COUNT(`id`) FROM `users` WHERE `username` = ?");
    $query->bindValue(1, $username);

        try
        {
            $query->execute();
            $rows = $query->fetchColumn();

            if($rows == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch(PDOException $e)
        {
            die($e->getMessage());
        }
    }

    public function login($username, $password)
    {
        global $bcrypt;

        $query = $this->_db->prepare("SELECT `password`, `id`, FROM `users` WHERE `username` = ?");

        $query->bindValue(1, $username);

        try
        {
            $query->execute();
            $data               = $query->fetch();
            $stored_password    = $data['password'];
            $id                 = $data['id'];

            #hasing the supplied password and comparing it with the stored hashed password.
            if($bcrypt->verify($password, $stored_password) === true)  //using the verify method to compare the password with hashed passwords
            {
                return $id;
            }
            else
            {
                return false;
            }
        }
        catch(PDOException $e)
        {
            die($e->getMessage());
        }
    }

i have been at this for almost half a day now and still couldnt figure this out. i think im missing something that might be very simple for you guys here but i just dont know what it is. i am hoping that comeone could help me with this. i would greatly appreciate it. thanks!

Link to comment
https://forums.phpfreaks.com/topic/287606-error-when-trying-to-login/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.