Jump to content

I need help with MYSQL JOINS


madjack87

Recommended Posts

Check out my code below and you will see what I am trying to do.

 

I cannot figure out what is going wrong.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mskordus/public_html/index.php on line 25

 

<?php
$result = mysql_query("SELECT * FROM students,teachers,specialEd,course,sched,notes WHERE
				   students.studentId=notes.studentId AND
				   students.studentId=sched.studentId AND
				   students.teacherId=teachers.teacherId AND
				   students.specialId=specialEd.specialId AND
				   students.courseId=course.courseId AND				
				   ");
echo "<table>";
while ($row = mysql_fetch_array($result)){
echo "<tr>";
	echo "<td>" . $row['students.fName'] . "</td>";
	echo "<td>" . $row['teachers.fName'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>

Link to comment
Share on other sites

you title says

I need help with MYSQL JOINS
, so your logic is correct, you know what you want to do, but you don't have a single JOIN in your query.

maybe you should read this: http://dev.mysql.com/doc/refman/5.0/en/join.html

 

Also: you query ends with AND

 

Ok I will check that out. Thank you. Yea I figured I must have some syntax incorrect, but I was having a hard time finding a good source that related to what I wanted to do.

 

I will report back.

Link to comment
Share on other sites

One of the problems I am having is when trying to echo the results because my tables have some identical column names.

 

<?php
$result = mysql_query("SELECT * FROM students LEFT JOIN teachers ON students.teacherId = teachers.teacherId");
echo "<table>";
while ($row = mysql_fetch_array($result)){
echo "<tr>";
	echo "<td>" . $row['fName'] . "</td>";
	//echo "<td>" . $row['teachers.fName'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>

 

The above code will work but the commented out section will not. How do I differentiate between teachers.fName and students.fName when trying to echo the results?

Link to comment
Share on other sites

you can set different names during your SELECT but you need the field names for that, so you won't be using SELECT *

 

instead you use something like :

 

"SELECT students.fName as SfName,teachers.fName as Tfname FROM students LEFT JOIN teachers ON students.teacherId = teachers.teacherId"

 

 

then u use the new names: $row['SfName'] and $row['TfName']

 

(SELECT * is bad anyway)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.