title: How to count Total Downline , Give me any Idea (MLM project)
body: <p>Anyone give me any Idea about How I can Count down-line for a MLM project (PHP/MySQL). Currently I'm using the below method. but when my database record become 300 or more, then it shows: maximum execution time of 30 second exceeded in line 51. (I think this happens for unlimited loop). please help me. what should be proper way?</p>
  
  <p><strong>MY CURRENT CODE:</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  require_once('configuration.php');
  
  /// main function which will return Total Node Count
  function nodecount($id) {
     $query = "SELECT * FROM ".memberlogtbl." WHERE locationid='".$id."' and topup &gt;'0'";
     $result = mysql_query($query);
     $count = mysql_num_rows($result);
  
     $_SESSION['hackx8'] = $_SESSION['hackx8'] + $count;
     while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
        nodecount($row["id"]);
     }
     mysql_free_result($result);
     return "";
  }
  
  $q_l = mysql_query("SELECT `left`,id FROM ".memberlogtbl." WHERE `left` &gt;'0' ORDER BY id ASC");
  while($updt_tbl_l = mysql_fetch_row($q_l)) {
     unset($_SESSION['hackx8']);
     $total_l = nodecount($updt_tbl_l['0']).$_SESSION['hackx8'] + 1;
     mysql_query("UPDATE ".memberlogtbl." SET totalleft='$total_l' WHERE id='".$updt_tbl_l['1']."'");
  }
  /// Update database with letest Total right Count
  $q_r = mysql_query("SELECT `right`,id FROM ".memberlogtbl." WHERE `right` &gt;'0' ORDER BY id ASC");
  while($updt_tbl_r = mysql_fetch_row($q_r)) {
     unset($_SESSION['hackx8']);
     $total_r = nodecount($updt_tbl_r['0']).$_SESSION['hackx8'] + 1;
     mysql_query("UPDATE ".memberlogtbl." SET totalright='$total_r' WHERE id='".$updt_tbl_r['1']."'");
  }
  ?&gt;
  </code></pre>