We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,697 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Cant fix the JOIN query of MYSQL using PHP

hi everyone..hope every1 is going great..well i'm a beginner in mysql and php and i cant fix the query as i desire..iam joining 3 tables here and displaying thre data..

1st i have displayed course names from the courses table
2nd i have displayed the subject names from wich the courses belong.
3rd i want to display the status (if any) of each course from request records table,
when the student wants an approval he just applies the course and the status (applied) is saved in request records with 2 foreign keys (coursesid & studentid) here when i display the 3rd table's status it shows only the rows of those courses for whom i've applied..but not the list of all courses.. please help.. i want it to display list of all courses + status if any...
this is offered-courses.php

<?php
   $con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con)
    {
   die('Could not connect: ' . mysql_error());
    }
mysql_select_db($dbname, $con);
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.subjectsid, subjects.id AS sid, subjects.subjectname, requestrecord.status FROM courses, subjects, requestrecord WHERE (courses.subjectsid=subjects.id AND courses.id=requestrecord.coursesid)");
echo "<table border='1' style='width:500px;'> <br />
<tr>
<th>Course Name</th>
<th>Subject </th>
<th>Action</th>
<th>Status</th>

</tr>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
    echo "<td>" . $row['title'] . "</td>";
    echo "<td>" . $row['subjectname'] . "</td>";
    echo "<td><a href='request-for-approval.php?id=".$row['cid']."& sid=".$row['sid']."'>Apply!</a></td>"; 
    echo "<td>" . $row['status'] . "</td>";
  echo "</tr>";
    }
echo "</table>";
mysql_close($con);
?>
2
Contributors
9
Replies
2 Days
Discussion Span
6 Months Ago
Last Updated
10
Views
Question
Answered
Riu 2009
Junior Poster
119 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

You mean like this:

SELECT courses.id AS cid, courses.title, courses.subjectsid, subjects.id AS sid, subjects.subjectname, requestrecord.status 
FROM courses, subjects 
LEFT JOIN requestrecord 
ON courses.id = requestrecord.coursesid
WHERE courses.subjectsid = subjects.id 
pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

i have to write this query in php and this is not the syntax in php..im a beginner..sorry for bothering u @pritaeas

Riu 2009
Junior Poster
119 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

@pritaeas the problem is fixed thanx :)

i want to show the applied status of only the student who is logged in... it shows multiple rows of applied status with same course names because different students have applied...how would i filter tht??? thanx in advance :)

Riu 2009
Junior Poster
119 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Add it to the WHERE clause. Without any sample data I cannot be more specific.

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

@pritaeas
in courses table i have following columns(id, title, description, subjectsid, accountsid)
in accounts table i have following columns(id, fname, lname, email, pwd,)
in subjects table i have following columns(id, subjectname)
in requestrecord table i have following columns(id, status, accountsid, coursesid)

(i want to display record status (if any) of only the student who is logged in and list of all courses with course title, subjectname, action (to apply) and status (if any)... what iam getting is record of all students who have applied... heres my query

$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.subjectsid, subjects.id AS sid, subjects.subjectname,  requestrecord.status, accounts.id
FROM courses JOIN subjects ON courses.subjectsid = subjects.id 
LEFT JOIN requestrecord 
ON courses.id = requestrecord.coursesid 
INNER JOIN accounts
WHERE accounts.id=".$_SESSION['id']);
echo "<table border='1' style='width:500px;'> <br />
<tr>
<th>Course Name</th>
<th>Subject </th>
<th>Action</th>
<th>Status</th>

</tr>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
    echo "<td>" . $row['title'] . "</td>";
    echo "<td>" . $row['subjectname'] . "</td>";
    echo "<td><a href='request-for-approval.php?id=".$row['cid']."& sid=".$row['sid']."'>Apply!</a></td>"; 
    echo "<td>" . $row['status'] . "</td>";
  echo "</tr>";
    }
echo "</table>";
mysql_close($con);
?>
Riu 2009
Junior Poster
119 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Am not sure what status is, but you can replace 'X' with what it should be. The query should look like this:

$result = mysql_query("
    SELECT courses.id AS cid, courses.title, courses.subjectsid, 
           subjects.id AS sid, subjects.subjectname, 
           requestrecord.status, 
           accounts.id
    FROM courses 
    JOIN subjects 
        ON courses.subjectsid = subjects.id 
    LEFT JOIN requestrecord 
        ON courses.id = requestrecord.coursesid 
        AND requestrecord.status = 'X'
    INNER JOIN accounts
        ON accounts.id = " . $_SESSION['id']
);
pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

@pritaeas still its showing the record of another students who has applied in the same subject...i would be thankful for ur response

Riu 2009
Junior Poster
119 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

The only way for me to reproduce would be an sql dump of the tables (structure and some data).

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

i've tried, this is how it is solved :)

$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.subjectsid, subjects.id AS sid, subjects.subjectname,  requestrecord.status
FROM courses JOIN subjects ON courses.subjectsid = subjects.id 
LEFT JOIN requestrecord 
ON courses.id = requestrecord.coursesid AND requestrecord.accountsid=".$_SESSION['id']);
Riu 2009
Junior Poster
119 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 6 Months Ago by pritaeas

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0976 seconds using 2.72MB