We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

I want to know if either email or username already exist on users table

hi guys, i want to know if users email or username already exist on my user table.' here is my code $checkfirst = Tblusers::findFirst(array("uname = '$uname'", "email = '$email'")); i think something wrong with my code.



26.3k
edited Jul '14

$checkfirst = Tblusers::findFirst(array(
    "uname = :uname: AND email = :email:",
    "bind" = array(
        "uname" => $uname,
        "email" => $email,
))); 

or


$checkfirst = Tblusers::findFirst(array(
    "uname = ?1 AND email = ?2 ",
    "bind" = array(
        1 => $uname,
        2 => $email,
)));


26.3k
Accepted
answer

I have read on this forum that in case you only need to know if such user exists and you do not need to know values of its fields it is better to use count instead of findFirst.

$checkfirst = Tblusers::count(array(
    "uname = :uname: AND email = :email:",
    "bind" = array(
        "uname" => $uname,
        "email" => $email,
)));