I need to filter data based on one table, but i dont know the correct usage for the select from function in php mysql. The below code shows what i am trying to do, but I think you can use multiple SELECT.
$query_Student = "SELECT Project_Title, Project_Lecturer FROM projects WHERE Project_id =
(SELECT Proj_id FROM project_course WHERE Cour_id = (SELECT Course_id from courses WHERE
Code = (SELECT Course FROM users WHERE Username = ".$_SESSION['MM_Username']."))"
Can anyone offer any assistance to what I need to write instead?
What you want to do is not exactly clear to me, but to make your query work you need to put single quotes around $_SESSION['MM_Username']
, like gogowitsch already said.
Also another closing paranthese is missing. So your query should look like this:
$query_Student = "SELECT Project_Title, Project_Lecturer FROM projects WHERE Project_id =
(SELECT Proj_id FROM project_course WHERE Cour_id = (SELECT Course_id from courses WHERE
Code = (SELECT Course FROM users WHERE Username = '".$_SESSION['MM_Username']."')))"
But...
That is not a very well written query.
I'm just guessing now, but maybe this is what you are looking for:
SELECT
p.Project_Title,
p.Project_Lecturer
FROM projects p
INNER JOIN project_course pc ON p.Project_id = pc.Proj_id
INNER JOIN courses c ON pc.Cour_id = c.Course_id
INNER JOIN users u ON c.Code = u.Course
WHERE u.Username = 'yourUserName';
If not, show us the tables you are using (do a SHOW CREATE TABLE projects;
and so on for every table you are using and post it here) and maybe what output you expect, then we can really help.
You might need to add quotes around the username. Can $_SESSION['MM_Username']
be a string?