<?php
// connect to the database
   
if(!($dblink=mysql_connect("shares.cems.uwe.ac.uk","cwstudent","cwstudent"))) {
     print(
"mysql_connect failed<br>\n");
     exit;
   }

   if(!(
mysql_select_db("Emp",$dblink))) {
     print(
"cannot connect to database $db");
     exit;
   }
   
$f .= "digraph hier {\n";


// get the hierarchy data
   
$query "select e.ename as ename, m.ename as mname from emp e , emp m where e.mgr = m.empno;";
   if(!(
$dbresult mysql_query($query,$dblink))) {
        print(
"<b>query failed</h4><br>\n");
        print(
mysql_error() . "<br>\n");
   }
// and generate the links
   
while($rep=mysql_fetch_object($dbresult)) {
        
$f .="$rep->ename  -> $rep->mname; \n"
   }

// get the emp dept links
   
$query "select ename, dname from emp natural join dept";
   if(!(
$dbresult mysql_query($query,$dblink))) {
        print(
"<b>query failed</h4><br>\n");
        print(
mysql_error() . "<br>\n");
   }
// and generate the links
   
while($rep=mysql_fetch_object($dbresult)) {
       
$f .= "$rep->ename  -> $rep->dname[style=dashed]; \n"
   }

// get the depts and make them square
   
$query "select dname from dept";
   if(!(
$dbresult mysql_query($query,$dblink))) {
        print(
"<b>query failed</h4><br>\n");
        print(
mysql_error() . "<br>\n");
   }
// and generate the links
   
while($rep=mysql_fetch_object($dbresult)) {
       
$f .="$rep->dname [ shape=box]; \n"
   }


   
$f .= "}";
   
mysql_close($dblink);

//set output to be GIF
   
header("Content-type: image/gif");
//execute the dot program and pass thru the output back to the client
   
passthru(" echo '$f' | /usr/local/graphviz/bin/dot -Tgif ");
   

?>