this is just a simple code.My problem here is that I can't get or display subject loads of a teacher.

            $sql = mysql_query("SELECT * FROM teachersubject WHERE teacherID ='$id'");
            while($test = mysql_fetch_array($sql))
            {
                $subjectid=$test['subjectID'];
                $getsubject=mysql_query("SELECT * FROM subject WHERE subjectID='$subjectid'") or die(mysql_error());
                $getthis=mysql_fetch_array($getsubject);
                echo "<option value=".$getthis['subjectID'].">".$test['subjectCode']."</option>";
            }
            mysql_close($conn);
            ?>

This in a form of List/Menu..thanks
Please I really need help... :(

Recommended Answers

All 3 Replies

try using mysql_fetch_assoc()

A quick debugging technique is to check the contents of variables at various stages using a combination of echo, die() and print_r() functions:

$sql = mysql_query("SELECT * FROM teachersubject WHERE teacherID ='$id'");
while($test = mysql_fetch_array($sql))
{        
    // inspect the contents of returned row
    echo(print_r($test, 1)) . '<br />';

    $subjectid=$test['subjectID'];

    // inspect the contents of subjectID
    echo "subjectID: $subjectid<br />";

    $getsubject=mysql_query("SELECT * FROM subject WHERE subjectID='$subjectid'") or die(mysql_error());

    // inspect the query (you can copy it to phpmyadmin)
    echo "getsubject: $getsubject<br />";

    $getthis=mysql_fetch_array($getsubject);

    // inspect the value of $getthis
    echo('getthis:' . print_r($getthis, 1));  

    // you can stop after the first loop to check the values for first row in your time
    die('----------------');

    echo "<option value=".$getthis['subjectID'].">".$test['subjectCode']."</option>";
}
mysql_close($conn);

This way you it will be easier to spot the errors.

You need to run your fetch_array through another while loop:

while ($getthis = mysql_fetch_array($getsubject)) {
echo "<option value=".$getthis['subjectID'].">".$test['subjectCode']."</option>";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.