php - simple login function using PDO -


im still in learning stages, banging head against walls looking clues. iv been reading manual no avail.

im building user log in system based on phpacadamy tutorial 'register & login'. use mysql_connect in tutorial, using pdo connection mysql.

1) function user_id_from_username should return user_id entry posted $username. mine not, im confused how return entry, , need little bit of guidance , explanation.

2) login function works, need return $user_id if true, can set session.

here code:

    <?php      function user_id_from_username(pdo $db, $username) {     $stmt = $db->prepare('select `user_id` `users` `username` = 1');     $stmt->bindparam(1, $username);     $stmt->execute();     return ($stmt->fetchcolumn());     }   //??? need function return `user_id` entry $username      function login(pdo $db, $username, $password) {     $user_id = user_id_from_username($db, $username);     $password = md5($password);     $stmt = $db->prepare('select count(`user_id`) `users` `username` = ? , `password` = ?');     $stmt->bindparam(1, $username);     $stmt->bindparam(2, $password);     $stmt->execute();     return (bool) $stmt->fetchcolumn();     }   //??? need function return $user_id if true (to set session)      //---------------------login.php-----------------------      if (empty($_post) === false) {     $username = $_post['username'];     $password = $_post['password'];     if (empty($username) === true || empty($password) === true) {     $errors[] = 'you need enter username , password.';     } else if (user_exists($db, $username) === false) {     $errors[] = 'we can\'t find username. have registered?';     } else if (user_active($db, $username) === false) {     $errors[] = 'you haven\'t activated account!';     } else {     $login = login($db, $username, $password);     if ($login === false) {     $errors[] = 'that username/password combination incorrect.';     } else {     die($login);       }     }     print_r($errors);     } 

so, according login script, after successful login (good username , password, , active account) should output $user_id integer: "die($login)".

(i didnt include other 2 functions: user_exists , user_active, because have them working properly)

it prints error array correctly, logs in ok, except next step.

thanks in advance helping me out!

you have put ? here

$stmt = $db->prepare('select `user_id` `users` `username` = 1'); 

should be

$stmt = $db->prepare('select `user_id` `users` `username` = ?'); 

edit

use function

function login(pdo $db, $username, $password) { $user_id = user_id_from_username($db, $username); $password = md5($password); $stmt = $db->prepare('select count(`user_id`) `users` `username` = ? , `password` = ?'); $stmt->bindparam(1, $username); $stmt->bindparam(2, $password); $stmt->execute(); if($stmt->fetchcolumn() > 0) {     return $user_id; } else {     return false; } } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -