Abhi Burk Abhi Burk - 3 months ago 6
MySQL Question

How Do I SELECT Values From The Database similar to the values of the login user

I got

user
Table which contains fields like
userName
,
department
and so on. So My question is if login user belongs to Computer Sci & Technology
department
then how do i select all the user who belongs to the Computer Sci & Technology
department
from the database

userName courseID department
sachin 1 Civil Engineering
virat 2 Computer Sci & Technology
rohit 2 Computer Sci & Technology

<form role="form" method="post" action="user/checkLogin.php">
<input type="text" name="u_ID" value="" placeholder="Unique-ID">
<input type="password" name="dob" value="" placeholder="Birth Date (DD/MM/YY)"> </form>


so the result should be virat and rohit

Answer

This is how it could be done. step by step

<?php
session_start(); 

//your connection
$link = mysqli_connect("localhost","root","","dbname") or die("Couldn't make connection.");

//assuming $userID is user id of logged in user
$userID = $_SESSION['userid'];

//table of user who has logged in
$query_loguser = mysqli_query($link,"SELECT * FROM `user` WHERE userid = '$userID'");
$row_loguser = mysqli_fetch_assoc($query_loguser);

//department of logged in user
$department = $row_loguser['department'];

//users from same department as logged in user
$query_depuser = mysqli_query($link,"SELECT * FROM `user` WHERE `department` = '$department'");
$row_depuser = mysqli_fetch_assoc($query_depuser);


?>

<?php do { ?>

<?php echo $row_depuser['userName']; //list all users who are from same department as logged in user ?><br />

<?php } while ($row_depuser = mysqli_fetch_assoc($query_depuser));   ?>
Comments