junkomatic Posted May 3, 2013 Share Posted May 3, 2013 Hi, im still in the early learning stages, banging my head against walls looking for clues. Iv been reading the manual to no avail. im building a user log in system based on the phpAcadamy tutorial 'Register & Login'. They use mysql_connect in the tutorial, but I am using a PDO connection to mysql. 1) the function user_id_from_username should return the `user_id` entry for the posted $username. mine does not, im confused about how to simply return the entry, and i just need a little bit of guidance and explanation. 2) the login function works, BUT i need it to return $user_id if TRUE, so that i can set the session. here is my code: function user_id_from_username(PDO $db, $username) { $stmt = $db->prepare('SELECT `user_id` FROM `users` WHERE `username` = 1'); $stmt->bindParam(1, $username); $stmt->execute(); return ($stmt->fetchColumn()); } //??? I NEED THIS FUNCTION TO RETURN THE `user_id` ENTRY FOR $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`) FROM `users` WHERE `username` = ? AND `password` = ?'); $stmt->bindParam(1, $username); $stmt->bindParam(2, $password); $stmt->execute(); return (bool) $stmt->fetchColumn(); } //??? I NEED THIS FUNCTION TO 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 to enter a username and password.'; } else if (user_exists($db, $username) === false) { $errors[] = 'We can\'t find that username. Have you registered?'; } else if (user_active($db, $username) === false) { $errors[] = 'You haven\'t activated your account!'; } else { $login = login($db, $username, $password); if ($login === false) { $errors[] = 'That username/password combination is incorrect.'; } else { die($login); } } print_r($errors); } So, according to this login script, after a successful login (good username and password, and active account) it should output the $user_id integer: "die($login)". It prints the error array correctly, it logs in ok, except for this next step.Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/277567-simple-login-function-using-pdo/ Share on other sites More sharing options...
junkomatic Posted May 3, 2013 Author Share Posted May 3, 2013 (I didnt include the other two functions: user_exists and user_active, because i have them working properly) Link to comment https://forums.phpfreaks.com/topic/277567-simple-login-function-using-pdo/#findComment-1427874 Share on other sites More sharing options...
junkomatic Posted May 3, 2013 Author Share Posted May 3, 2013 and i just noticed in line 2 that '1' should be '?'. but still, the problem has to do with the return lines missing something critical, i think. Link to comment https://forums.phpfreaks.com/topic/277567-simple-login-function-using-pdo/#findComment-1427882 Share on other sites More sharing options...
junkomatic Posted May 3, 2013 Author Share Posted May 3, 2013 ok i fixed it: function login(PDO $db, $username, $password) { $user_id = user_id_from_username($db, $username); $password = md5($password); $stmt = $db->prepare('SELECT COUNT(`user_id`) FROM `users` WHERE `username` = ? AND `password` = ?'); $stmt->bindParam(1, $username); $stmt->bindParam(2, $password); $stmt->execute(); if($stmt->fetchColumn() > 0) { return $user_id; } else { return FALSE; } } just needed to add the if/else at the end of this login function duh! Link to comment https://forums.phpfreaks.com/topic/277567-simple-login-function-using-pdo/#findComment-1427884 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.