0
votes
<?php

function login($database, $username, $password) {

$query = "SELECT * FROM `users` WHERE username=':username'";
$query = $database->prepare($query);

$query->execute(array(':username' => $username));

$rowcount = $query->rowCount();

    if($rowcount == 1){
        $row = mysql_fetch_array($query);
        $dbPass      = $row["password"];


            if($password == $dbPass){
                session_start();

                $dbId        = $row["id"];
                $dbUser      = $row["username"];
                $dbEmail     = $row["email"];
                $dbFirstname = $row["firstname"];
                $dbLastname  = $row["lastname"];

                        //Register Session Variables
                $_SESSION['id']        = $dbId;
                $_SESSION['username']  = $dbUser;
                $_SESSION['email']     = $dbEmail;
                $_SESSION['name']      = $dbFirstname." ".$dbLastname;

                return true;

            } else
                return false;
    } else
        return false;
}

?>

This is a PHP code snippet from a project I am globally converting to PDO. This is the functions.php file for the login page. Obviously it is not fully converted to PDO so don't criticize that, but basically in the login.php file I have it access this method, and pass the database(which is required in), the username, and the password from the form. I setup a basic query to find all users with the username input of the form. Then i prepare, and execute the query. I then need a row count, so I setup a $rowcount variable running the rowCount() method on the query, but the code does not move past there. The rowcount is == 0 when I echo it out so it won't proceed to the following if statement. Am I doing something wrong with the PDO or something? Or the rowCount(). My suspicion is that perhaps I am calling the rowCount() too late, so I tried moving it up before I execute the $query but no luck. Thank you!

___EDIT___

<?php
session_start();


function login($database, $username, $password) {

$query = "SELECT * FROM `users` WHERE username=':username'";
$query = $database->prepare($query);

$query->execute(array(':username' => $username));

    if($query->rowCount()){
        $row = $query->fetch();
        echo $row;
        $dbPass      = $row["password"];


            if($password == $dbPass){

                $dbId        = $row["id"];
                $dbUser      = $row["username"];
                $dbEmail     = $row["email"];
                $dbFirstname = $row["firstname"];
                $dbLastname  = $row["lastname"];

                        //Register Session Variables
                $_SESSION['id']        = $dbId;
                $_SESSION['username']  = $dbUser;
                $_SESSION['email']     = $dbEmail;
                $_SESSION['name']      = $dbFirstname." ".$dbLastname;

                return true;

            } else {
                return false;
            }
    } else {
        return false;
    }
}

?>
1
Again I know that I am using mysql() code after the if statement, but my problem now is that I cannot even get the code to run the if statement. - user3412869
You can't mix them like that. - thescientist
Obviously it is not fully converted to PDO - It's pointless posting this as a question then. - The Blue Dog
Besides the answer(s) given, remove the quotes WHERE username=':username'"; - Funk Forty Niner
As an aside, you should hash your passwords. - Mike Brant

1 Answers

1
votes

Don't mix pdo and mysql_ functions together. NEVER!

Don't store password in plain text. NEVER! Instead try Password_compat !

First:

Is to replace

$row = mysql_fetch_array($query);

with

$query->fetchAll(PDO::FETCH_ASSOC)

Second:

session_start() should appear at the top of your script, not inside your function.

Third:

Is to replace

   $rowcount = $query->rowCount();
   if($rowcount == 1){
     //
   }

with this:

if($query->rowCount()){}

Fourth:

This is BAD!!

           return true;

            } else
                return false;
    } else
        return false;
}

Always, use a complete delimiter. You are instilling a bad-codding practice, that will haunt you for life.

Simple do

if($foo){
    if(){
       //do something
    }else if{
       //do something
    }else{
       //do something
    }
}

Fifth:

~Not good, but definitely better that your approach.

function small_query(pdo $pdo, $query, array $value){
    $stmt = $pdo->prepare($query);
    $stmt->execute($value);
    return $stmt->fetchAll(); 
}

$pdo = new PDO('mysql:host=localhost; dbname=foo', 'root', 'pass'); 

$result = small_query($pdo, "SELECT * FROM users WHERE name = ?", array($_POST['name']))

EDIT.

Since you seem to love your code so much, I have done it your way. Try this:

<?php

    session_start();

    function login($database, $username, $password){
        $query = "SELECT * FROM users WHERE username = ?";
        $stmt  = $database->prepare($query);
        $stmt->execute(array($username));

        if($stmt->rowCount()){
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC); 

            $_SESSION["id"] = $result["id"];
            $_SESSION["username"] = $result["username"];
            $_SESSION["email"] = $result["email"];

            return true; 
        }else{
            return false; 
        }
    }