Question

I build a family tree. The table looks like this:

id------firstname------mother------father-------kids

this is the code for person detals page:

$query = mysql_query(" SELECT family.id, family.firstname, mother.firstname AS mother, father.firstname AS father, kids.firstname AS kids

FROM family

JOIN family AS mother ON mother.id = family.mother JOIN family AS father ON father.id = family.father JOIN family AS kids ON kids.id = family.kids

WHERE family.id=".$_GET['id']

$row = mysql_fetch_array($query,) or die(mysql_error());

          echo "First Name: ".$row['firstname']."<BR>";
          echo "Mother Name: ".$row['mother']."<BR>"; 
          echo "Father Name:  ".$row['father']."<BR>"; 
          echo "Kids: ".$row['kids']."<BR>";
          echo "<BR>";

the result is:
First Name : XXXXX
Mother Name : YYYYY
Kids : AAAAAAA

The problem is when one of the fields are empty (if some person have no kids), script stops. and I get only:

First Name :
Mother Name :
Kids :

Any solution?

Was it helpful?

Solution

If the fields aren't required to have a value you should use LEFT JOINinstead of JOIN although this can result in quite heavy loading, depending on your amount of data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top