2 Replies - 310 Views - Last Post: 08 February 2014 - 06:31 AM

#1 Desertt  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 27-October 13

How to check type of user in PHP using Mysql

Posted 08 February 2014 - 05:09 AM

Hi i have 2 type of user premium and supplier i want after user logged in to know what is the type of user and redirect it to a page based on his type after login i am using this code
<?php
session_start();
if(isset($_SESSION['username']))
{
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="foryou"; // Database name 
$tbl_name="login"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$type = $_GET['type'];
$sql = "SELECT * FROM login WHERE type = 'Supplier'";
$Supplier = mysql_query($sql);
if ($Supplier === false)
{
  // IN DEVELOPMENT - tell me what went wrong
  echo $sql . '<BR>' . mysql_error();
  header("location:logout.php");
} 
else 
{
  $Supplier = mysql_result($Supplier, 0);
  header("location:logoutu.php");
}
}
else
{
echo '<ul id="login_menu">';
}
if(isset($_SESSION['user_id'])) 
{
echo '<li><a href="logout.php">LOGOUT</a></li>';
} 

else 
{
echo '<li><a href="login.php">LOGIN</a></li>';
echo '<li><a href="registration.php">REGISTER</a></li>';
}
echo '</ul>';

?>



But the problem is that what ever the user type it always take him to logoutu how can i define the user type ?

Is This A Good Question/Topic? 0
  • +

Replies To: How to check type of user in PHP using Mysql

#2 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

Re: How to check type of user in PHP using Mysql

Posted 08 February 2014 - 05:18 AM

You'll need to debug this code to see where the error is. First you want to comment out the line which redirects the user to logoutu.php. That way you can stay on the page long enough to find out what went wrong. Then you want to 'echo' the value of $Supplier after you do the database query, to make sure it's what you expect it to be. I'm assuming the only options are true or false.

Below the $Supplier = mysql_query($sql); line, add the line echo "Supplier? ".$Supplier;

Then reload the page and tell is what you get.

I'm not sure you want the line $Supplier = mysql_result($Supplier, 0);, and I'm also pretty sure your query is wrong, but we'll get to that afterwards.

Edit: Can you explain the layout of your database table?

This post has been edited by Ntwiles: 08 February 2014 - 05:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3716
  • View blog
  • Posts: 5,976
  • Joined: 08-June 10

Re: How to check type of user in PHP using Mysql

Posted 08 February 2014 - 06:31 AM

First of all, that code doesn't actually fetch the info on the user. It just fetches all rows where a user has the type "Supplier". You need to modify the SQL to fetch only data on the logged in user, which is presumably identified by the "username" session value?

Second, You really don't want to do SELECT * FROM and then use $field = mysql_result($result, 0);. That call just returns the first field in the first row, which with a wildcard selector may well not be the field you want. - Always be specific when selecting data, like SELECT id, type FROM, and then fetch them by name, using the mysql_fetch_assoc and accessing the field by name: $row["type"].


Having said that, the old mysql_query family of functions is obsolete and shouldn't be used today. Look into PDO or MySQLi to replace them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1