Hi, I need something like recursion wherein the query repeats itself using it's result to start the NEXT - SAME - QUERY.

I made 1 but it just simple reecho the same result.

// KUNIN ANG ID NI KURIMAW
$root = $_GET['id'];

$result = mysql_query("SELECT COUNT(*) as count, id, sponsor FROM agents WHERE sponsor = '$sponsor'") 
or die(mysql_error()); 
while ($row = mysql_fetch_array($result)){
$sponsor = $row['id'];

// RIGHT HERE, IWANT TO REPEAT THE SAME QUERY USING THE "RESULT $SPONSOR" IN THE NEXT WHERE STATEMENT

}

Thanks a lot geeks!

I am a bit confuded as to why you would want to run the query again? The where sponsor = $sponsor should limit the results so the $sponsor you return would be the same as the $sponsor you originally queried for.

I am a bit confuded as to why you would want to run the query again? The where sponsor = $sponsor should limit the results so the $sponsor you return would be the same as the $sponsor you originally queried for.

On the second query with the same functionality, I want to replace the "$sponsor" in the WHERE Statement with the $sponsor ( result from the 1st query )

Like liamfriel said, this will indefinitely get the same table row over and over again.

Member Avatar for rajarajan2017

1. What is the initial value of $sponsor? Where do you get from?
2. The below is the recursive fn but never going to be end bcoz the purpose of your part is unclear. Everytime it goes to get the id and and the id is stored in sponsor (sponsor is equal to id?)
3. Somewhat confusing...

<?php
processfn($sponsor);

function processfn($sponsor) {
	$result = mysql_query("SELECT COUNT(*) as count, id, sponsor FROM agents WHERE sponsor = '$sponsor'") 
	or die(mysql_error()); 
	while ($row = mysql_fetch_array($result)){
	$sponsor = $row['id'];
	}
	processfn($sponsor)
}
?>

The initial $sponsor is the $root = $_GET.. Now, the result of the first query using "$root as sponsor" will be passed down to repeat same query. Only, the result on the first will be used as $sponsor in the WHERE statement.. Thanks guys!

Member Avatar for rajarajan2017

Yes, I understood a little. Lets try with the above recursive fn

i think tried it and i think its working. Is there a possibility wherein i can specify how many times the recursion will be?

Member Avatar for rajarajan2017

Good to hear that, Yes you can....

<?php
processfn($sponsor);

function processfn($sponsor) {
	$result = mysql_query("SELECT COUNT(*) as count, id, sponsor FROM agents WHERE sponsor = '$sponsor'") 
	or die(mysql_error()); 
	if (mysql_num_rows($result) > 0) { 
		while ($row = mysql_fetch_array($result)){
			$sponsor = $row['id'];
		}
		processfn($sponsor)
	}
}
?>

It will work only if any of the id is not availble in the database.

Thank you so Much!

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.