cougar23 Posted June 11, 2008 Share Posted June 11, 2008 I have a table in my database(USERS), with three fields MEMBER_ID, PASSWORD, and USER_TYPE. I'm trying to store the results of the query and then need to redirect to a different directory based on the result's USER_TYPE. I've tried using bind_result() and then doing a switch off of $usertype, but apparently $usertype isn't populated. What am I doing wrong, or is there a better way to do this? function validateUser($userid, $password) { //attempt database connection to execute query and attempt match with userid and password require('db_fns.php'); if( $db = openConnection('guest', 'guest') ) { //build and execute query $stmt = $db -> prepare('SELECT member_id, password, user_type FROM users WHERE member_id = ? AND password = ?'); $stmt -> bind_param("ss", $userid, $password); $stmt -> execute(); $stmt -> store_result(); $num_rows = $stmt -> num_rows; //if no match occurred, userid and/or password user entered are NOT valid in the database //set error message and return to login page if($num_rows == 0) { session_start(); $_SESSION['errorMsg'] = 'user ID and/or password or incorrect; Please try again'; header('Location: ./'); } //if a match has occurred, the user is a valid user; get the user's type and //assign validUserId, validUserPassword, validUserName, validUserType SESSION variables else if ($num_rows >= 1) { //TODO $stmt->bind_result($userid, $password, $usertype); echo $userid; echo $password; echo $usertype; } //close prepared statement and database connection $stmt -> close(); closeConnection($db); } } Quote Link to comment Share on other sites More sharing options...
btherl Posted June 12, 2008 Share Posted June 12, 2008 The first thing to do is to check your method calls for errors, especially the call to bind_result(). If that fails then you would expect $usertype to be empty. The other thing I would check is that the data is not empty in the database itself. Quote Link to comment Share on other sites More sharing options...
cougar23 Posted June 12, 2008 Author Share Posted June 12, 2008 I kno the database is populated. It has only the one entry which i'm testing. Also, the userid and password variables are being set (although they could still be set from the code prior to bind_result). I did a check and the bind_result line is returning 1/true....any other ideas as to what's going wrong? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted June 12, 2008 Share Posted June 12, 2008 I'm assuming that you are either using an abstraction layer similar to the maxdb objects, or you are using maxdb (http://www.php.net/maxdb-stmt-bind-result). In the either case, try using: $stmt->bind_result($userid, $password, $usertype); $stmt->fetch(); echo $userid; echo $password; echo $usertype; Quote Link to comment Share on other sites More sharing options...
cougar23 Posted June 12, 2008 Author Share Posted June 12, 2008 That fetch() line did it; thanks to everyone! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.