0

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?

1 Answers1

0

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.

Vapire
  • 4,568
  • 3
  • 24
  • 41
  • Thank you, any solution to make the loading faster? – user1740512 Oct 12 '12 at 11:56
  • Well, you could split it up in several SQL queries and test beforehand if there's something to select. Then of course indexes. And here's a link for optimizing left and right joins: http://dev.mysql.com/doc/refman/5.0/en/left-join-optimization.html – Vapire Oct 12 '12 at 12:06