get the value from derived table


in the walikelas table i have field :id_teacher,name,password
in the kelas table i have field :id_class, class_name, and id_teacher.

$query=("SELECT w.name, k.id_teacher, k.class_name FROM walikelas w, kelas k WHERE w.id_teacher = k.id_teacher");


my question is how we get the value from w.name,k.id_teacher,k.class_name and then we can put it in the variabels. Thank you.

$query=("SELECT w.name, k.id_teacher, k.class_name FROM walikelas w, kelas k WHERE w.id_teacher = k.id_teacher");

my question is how we get the value from w.name,k.id_teacher,k.class_name and then we can put it in the variabels. Thank you.

Now you need to execute the query, save the result, and get the values using one of the mysql_fetch functions (mysql_fetch_row(), mysql_fetch_assoc(), mysql_fetch_object() - go to php.net for more details).

To do that, you would do something like this...

$result = mysql_query($query, $db);

//  This loop grabs the output one row at a time,  until there is none
while ($data = mysql_fetch_object($result)) {
    echo "Name - " . $data->name . "<br />";
    echo "Class - " . $data->class . "<br /.";
}

The mysql_query saves the result in $result, then you fetch the results into an object one at a time.

That object contains all of the information from your mysql query, with a property named after the item in your query.

So $data->name is your "name," $data->id_teacher is your "id_teacher," and $data->class_name is your "class_name." In the example I just echo'ed them, but you could just as easily save them in another variable for later manipulation.

Good luck,
- Walkere

This article has been dead for over six months. Start a new discussion instead.