...

View Full Version : Help combining data from 2 tables... I keep failing!



DBookatay
03-18-2013, 07:13 AM
I am trying to make a printable enrollment list of kids enrolled in each class, by joining the "enrollment" table and the students table.
This code works:

$id = $_GET['id'];

$query = 'SELECT students.nmL, students.nmF FROM students INNER JOIN enrollment ON students.id=enrollment.s1_AM ORDER BY students.nmL';
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
for($x = 0; $row = mysql_fetch_array($result); $x++) {
$names .= '<p>'.$row['nmF'].' '.$row['nmL'].'</p>';
}

but as soon as I add a "Where" clause:

SELECT students.nmL, students.nmF FROM students INNER JOIN enrollment ON students.id = enrollment.s1_AM WHERE enrollment.s1_AM = '.$id;

I get these errors:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in/home/dbookatay/superscienceenrichment.com/Admin/print.php on line 212

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in/home/dbookatay/superscienceenrichment.com/Admin/print.php on line 213

I am trying to pull all the students id from the s1_AM fields in the enrollment table, then print their first and last names from the students table. (There are 20 different classes, so I need the where clause, or else (my working code) prints all the results from all the courses, not just the one I want.)

(I've attached a screen cap of what I am trying to do)
Can someone please help, I have been struggling with this for hours now.

abduraooft
03-18-2013, 08:32 AM
I get these errors:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in/home/dbookatay/superscienceenrichment.com/Admin/print.php on line 212
Add proper error checks like

$result = mysql_query($query) or die(mysql_error()); so hat you may a description about the error in your query.

Old Pedant
03-18-2013, 08:30 PM
Also, now about DEBUG DEBUG DEBUG?


$sql = "SELECT students.nmL, students.nmF FROM students INNER JOIN enrollment "
. " ON students.id = enrollment.s1_AM "
. " WHERE enrollment.s1_AM = ' . $id;
echo "DEBUG SQL: $sql <hr/>\n";
...

And then see if what debug shows you makes sense.

If you still can't make sense of it, then show us a "dump" of a few rows from each of the two tables. Separately.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum