Jump to content

bind_result to store DB fields


cougar23

Recommended Posts

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);
	}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.