Keisuke Miyagi Keisuke Miyagi - 5 months ago 23
PHP Question

Show a group project to multiple users mysql

I have two tables the group_table where all the groups information is stored.

And i have a group_person_table where all id's which correlate to groups

I only want to display the group information to multiple users if they match in group_person_table

the database looks like this :

Group_table
person_id, group_id, groupname, groupdesc.

Group_person_table
person_group_id, group_id, person_id

currently i only have the following query:

"SELECT * FROM group_table WHERE person_id = $person_id" IN (SELECT person_id FROM group_person_table WHERE person_id =$person_id);


I currently am able to display all of the groups to all of the users but i only want to display the groups to the users if they're the same in group_person_table so user A and B are in group A so they both see Group A, if user A is in Group B, then User B wont see group B!

I'm displaying calling upon the data in OOP


public function get_project($person_id){
//$sql3="SELECT * FROM group_table WHERE person_id = $person_id ";
$sql3="SELECT * FROM group_table WHERE person_id = $person_id";
$results = mysqli_query($this->db, $sql3);

Answer

You have an error in the below syntax

"SELECT * FROM group_table WHERE person_id = $person_id" IN (SELECT person_id FROM group_person_table WHERE person_id =$person_id);

Try to alter you query

SELECT * FROM group_table WHERE person_id IN (SELECT person_id FROM group_person_table WHERE person_id =:person_id);

Where :person_id is your bound parameter using mysqli

Since you have not posted any actual php code or database schema (please provide one to answer this better) this fiddle might not be what you have in your setup.