Can you suggest me the best way to get what I want from my database please? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can you suggest me the best way to get what I want from my database please?

Hello. I am making a complex web app and I got stuck on one little problem. I need to knkw how to get certain input out of my database in a elegant way. I hav two tables there. One of them is named "classes" and has two columns: id and name. The second table is called "users" and has 3 columns: id, name, memberIn. memberIn is string that contains id's of classes the user has access to. Id's of classes are separated with commas (such as 1,2,4). I need to get names of the classes a user DOESN'T have access to. Example: users: 1 - John - 2,4 classes: 1 - 3.B 2 - 7.A 3 - 5.D 4 - 1.C output for John: 3.B, 5.E. I already have a code that works a bit, but it's unstable and too long and ugly.

30th Jul 2018, 9:00 PM
Jan Štěch
Jan Štěch - avatar
1 Answer
0
I get ID's of classes from the memberIn column with this code: /*Connecting to database and saving connection to variable $connection*/ $query = 'SELECT memberIn FROM users WHERE name="John"'; $result = mysqli_query($connection, $query); $result = mysqli_fetch_array($result); $result = $result['memberIn']; $values = explode(',',$result); /*$values now contains ID's of classes John is already member in.*/
30th Jul 2018, 9:06 PM
Jan Štěch
Jan Štěch - avatar