michaelkirby Posted March 23, 2010 Share Posted March 23, 2010 Hi, I have a table for users which has two types of users. In the table there is a usertypeid which gets set to either 1 or 2 depending on what type of user is registered. When a user logs in to the website I have set it up to have a profile page which allows them to see all the details that they had input. I want to have links on the profile page to enable them to carry out tasks, but the links will be different for the different users that log in. So i needed a way to hide them, so I was thinking using an IF statement.... IF(usertypeid == 1) { Display link; } However I'm not sure how to get the usertypeid from the users table of the user logged in and assign it to the variable. Any ideas???? I managed to use the username from the SESSION variable to do this, but I need it for usertypeid column. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/196215-mysql-database-and-php-variable-question/ Share on other sites More sharing options...
zeodragonzord Posted March 23, 2010 Share Posted March 23, 2010 Set the usertypeid into the $_SESSION variable the same way you set the username. Assuming that the usertypeid is in the same table as your username... Not tested... //Get the username from the database $query = "SELECT usertypeid FROM user WHERE username='{$username}'"; $result = mysql_query($query); //Assign the username into the $_SESSION $row = mysql_fetch_array($result, MYSQL_ASSOC); $_SESSION['usertypeid'] = $row['usertypeid']; //Do something depending on the usertypeid switch($_SESSION['usertypeid']) { case 1: { //Do something break; } case 2: { //Do something else break; } default: { //This is probably a usertypeid that we do not support or the result we're getting from the database is incorrect. break; } }//switch Those are the basics. You'll want to clean your variables before running a MySQL statement or use Pear MDB2. Quote Link to comment https://forums.phpfreaks.com/topic/196215-mysql-database-and-php-variable-question/#findComment-1030526 Share on other sites More sharing options...
dooper3 Posted March 23, 2010 Share Posted March 23, 2010 Personally I would do as zeodragonzord suggests, however, if you want to leave your session intact as you've set it before, you could do as follows. You run a query like so: $result = mysql_query('SELECT * FROM users WHERE username = '.addslashes($_SESSION['username']).' LIMIT 1'); Then check if the result returned one row: if (mysql_num_rows($result) == 1) If so you get the data: $user_data = mysql_fetch_assoc($result); Then you do your if / else links: if ($user_data['usertypeid'] == 1) //do something else //do something else Overall it should look something like this: $result = mysql_query('SELECT * FROM users WHERE username = '.addslashes($_SESSION['username']).' LIMIT 1'); if (mysql_num_rows($result) == 1) $user_data = mysql_fetch_assoc($result); if (is_array($user_data) && $user_data['usertypeid'] == 1) //do something elseif (is_array($user_data) && $user_data['usertypeid'] == 2) //do something else else //do something for non-logged in users Quote Link to comment https://forums.phpfreaks.com/topic/196215-mysql-database-and-php-variable-question/#findComment-1030545 Share on other sites More sharing options...
michaelkirby Posted March 24, 2010 Author Share Posted March 24, 2010 Thanks both of you for your input!! Much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/196215-mysql-database-and-php-variable-question/#findComment-1030952 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.