0

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!

4
Contributors
9
Replies
10
Views
7 Years
Discussion Span
Last Post by codewalkz
0

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.

0

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 )

0

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)
}
?>
0

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!

0

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

0

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.

Edited by rajarajan07: Added comment

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.