$query = $db->query("SELECT *
FROM users
ORDER BY username ASC");
while($user = $query->fetch_object()) {
$if_inside = $db->query("SELECT member_id
FROM project_members
WHERE member_id='$user->id'
UNION
SELECT admin_id
FROM project_admins
WHERE admin_id='$user->id'")->num_rows;
if($if_inside < 1) { // if not already in project
?>
<option value="<?php echo $user->username; ?>">
<?php echo $user->username; ?> </option>
<?php
}
}
?>
How would I optimize the code above? If I have 100 users in my database then it would query 100 times, and it is already slow (i have 2 users)
Is there a better way to check if an user is already in the project?
idof said user to see if they are in the project? Or you could do some sort ofJOINproject_membersandproject_admins, thats why I am using anUNION. How would I go about matching two values received from the query TO the user id?