Abhi Burk Abhi Burk - 11 months ago 36
MySQL Question

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

I got

Table which contains fields like
and so on. So My question is if login user belongs to Computer Sci & Technology
then how do i select all the user who belongs to the Computer Sci & Technology
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 Source

This is how it could be done. step by step


//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));   ?>