i must be able to extract the results of this query and use it as another value for "Parent" in the succeeding "THE SAME QUERY". Right here, the value of parent in WHERE clause is "1". Now, I want to create the same query but the value of the parent is the result of the query below.

Thanks a lot! ^_^

//RIGHT HERE I GET THE TOTAL OF LEVEL  RESULT OF LEVEL1
$result = mysql_query("SELECT COUNT(*) FROM agents WHERE sponsor = '$parent'")or die(mysql_error());  
$output = array();
while ($row = mysql_fetch_array($result)) {
$lvl1 = $row['COUNT(*)'];
echo "<b>Total Level 1 Downlines</b> : ";
echo " <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>$lvl1 <br><br>";
}
//NOW, I GET THE ID OF FIRST LEVEL RESULTS SO I CAN USE IT AS PARENT OF THE SAME QUERY ON LEVEL 2
$result = mysql_query("SELECT id, lastname FROM agents WHERE sponsor = '$parent'")or die(mysql_error());  
$output = array();
while ($row = mysql_fetch_array($result)) {
  $output[] = "sponsor = '".$row['id']."' ";
}
$newparent = implode(' or ', $output);

I may be wrong, but from a SQL point of view if you use a double-ampersand (&&)(I'm not sure if $ = &) it will prompt for the user to input a value and then keep the value in session until you UNDEFINE it.

meaning...

//RIGHT HERE I GET THE TOTAL OF LEVEL  RESULT OF LEVEL1
$result = mysql_query("SELECT COUNT(*) FROM agents WHERE sponsor = '$parent'")or die(mysql_error());

should become:

//RIGHT HERE I GET THE TOTAL OF LEVEL  RESULT OF LEVEL1
$result = mysql_query("SELECT COUNT(*) FROM agents WHERE sponsor = '&&parent'")or die(mysql_error());

and can be later referenced with a single-ampersand (&):

...
//NOW, I GET THE ID OF FIRST LEVEL RESULTS SO I CAN USE IT AS PARENT OF THE SAME QUERY ON LEVEL 2
$result = mysql_query("SELECT id, lastname FROM agents WHERE sponsor = '$parent'")or die(mysql_error());

Forgive me if I misinterpretted your problem or mixed up the syntax indicators, I'm not very familiar with SQL in PHP, but that is how I use it in SQL.

I see your point. i am not familiar when it comes to SQL. i don't even know if it should be compared. Somehow, the code I provided above is able to show the result. What i want to do now is to use that result as parent of another query which is just the same as above ONLY, the parent value will be the result of the code above.

And one more thing, i need to have the recursive and repeat it at least 50x

Thanks for the reply bro. Appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.