My review_shared
table looks like this. The column names are review_shared_id
, cat_id
, review_id
, user_id
, contact_id
.
I'm trying to figure out a way of seeing if $cat_id
is shared by users other than the current user, $user_id
, who in this case is 10219
.
For example $cat_id
is 188
. Is this shared with users other than 10219
? Yes - it's shared with user_id
number 3729
.
I'm not sure how to proceed, would be grateful for some help.
What I have so far is:
//check if $cat_id is being used by users other than $user_id
//we do this in the review_shared table
$query3 = "SELECT * FROM review_shared WHERE cat_id = ?";
$stmt3 = $con->prepare($query3) or die(mysqli_error($con));
$stmt3->bind_param('i', $cat_id) or die ("MySQLi-stmt binding failed ".$stmt3->error);
$stmt3->execute() or die ("MySQLi-stmt execute failed ".$stmt3->error);
$result3 = $stmt3->get_result();
while ($row = $result3->fetch_assoc()) {
//get the corresponding user_id for that cat_id
$matching_user_id = $row["user_id"];
If ($cat_id is shared by users other than the current user, $user_id) {
}
else {
}
Just add it to the where clause using a not equal operator (<>
or !=
will both work in MySQL). The query below will return all distinct userids. I added distinct, because it seems that there are multiple rows for cat 188 and the same userid, so without distinct
, you would get that same user id multiple times. This query will return 0 rows if there are no other users that this cat is shared with.
SELECT DISTINCT
user_id
FROM review_shared
WHERE cat_id = ?
AND user_id <> ?
If you just want to know how many, you can count it. The query below will return one row with one value. That value is the number of users it is shared with apart from the given user id. The query will return the value 0
if there are no such users. distinct
is added within the count, to count each distinct user_id only once. Otherwise your example data would result in 4
, because cat 188 is shared 4 times with the same user.
SELECT
COUNT(DISTINCT user_id) AS user_count
FROM review_shared
WHERE cat_id = ?
AND user_id <> ?