I have the following table
uId | groupId
==============
1 12
3 66
How can I write a mySQL statement to check if a user exists in a group?
What I've Tried:
SELECT FROM usertable(uId) WHERE usertable.uId = $uId
the above query only checks if the user exists but doesn't check whether the user belongs to a particular group.
Just append that condition to your where
clause, for example:
SELECT uId
FROM usertable
WHERE usertable.uId = 1
AND groupId = 12
Simply add the groupId
parameter to your query WHERE
clause.
$userId = x; // some user id
$groupId = y; // some group id
$con; // your mysqli instance
if (!$stmt = $con->prepare('SELECT 1 FROM usertable WHERE uId = ? AND groupId = ?')) {
throw new Exception($con->error, $con->errno);
}
$stmt->bind_param('ii', $userId, $groupId);
if (!$stmt->execute()) {
throw new Exception($stmt->error, $stmt->errno);
}
if ($stmt->fetch()) {
// user exists and is in group
}
$query = mysql_query("Select from usertable(uId) where usertable.uId = $uId and groupId=$gid");
if(mysql_num_rows($query)!=0){
echo "user exists!";
}else
{
echo "user not exists!";
}