Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help combining data from 2 tables... I keep failing!

    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:
    Code:
    $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:
    Code:
    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.
    Attached Thumbnails Attached Thumbnails Help combining data from 2 tables... I keep failing!-sql-help.jpg  

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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
    PHP Code:
    $result mysql_query($query) or die(mysql_error()); 
    so hat you may a description about the error in your query.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Also, now about DEBUG DEBUG DEBUG?
    Code:
    $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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •