0

I want to create a recursive query scripts that outputs the ID_Col2 which will be used as parameter for the next recursive query. Here my code which actually stops only at first cycle:

function recursiveQuery ( $ID, $numRows ) {                                         
  for($i=0;$i<=$numRows;$i++) {                                         
    $dbConnect = new MySqlConnect();
    $dbConnect->connect();      
    $query = $dbConnect->query("SELECT ID_Col1, ID_Col2 FROM wbs WHERE ID_Col1 = '" . $ID . "' AND ID_Col2 != '0' ORDER BY ID ASC");
    $n = mysql_num_rows($query);
    $result = $dbConnect->extractObject($query);    

    for($x=0;$x<count($result);$x++){
      $r = $result[$x]->ID_Col2;
      echo $r . '<br>';
      return recursiveQuery( $r, $n );
    }

    $dbConnect->disconnect();                                               
  }
}

echo recursiveQuery( '6765', '25' );

So how should I modify this script to make it works?

4
  • Doing database queries in a loop in a recursive function seems like a very bad idea. There surely is a better way to accomplish what you want to do, but you would have to specify what you want to do exactly. Commented Apr 28, 2014 at 13:51
  • Could you please clarify this a bit? What's inside wbs? What's the exact output you desire? Commented Apr 28, 2014 at 13:51
  • 1
    not to answer but you really should use PDO for databases Commented Apr 28, 2014 at 13:53
  • @Segiu Parashiv I have two columns with two IDs. The first column are fathers, the second are sons. Sons, can be also fathers. So, I want to use the ID_Col2 as parameter for the next query, in order to get all sons in a tree structure. Commented Apr 28, 2014 at 13:58

2 Answers 2

1

You open Databases connections recursively... :

function recursiveQuery ( $ID, $numRows ) {
  $dbConnect = new MySqlConnect();
  $dbConnect->connect();   
  
  for($i=0;$i<=$numRows;$i++) {
    // [...]    

    for($x=0; $x < count($result); $x++){
      // [...]
      return recursiveQuery( $r, $n );
    }

    $dbConnect->disconnect();                                               
  }
}

I can't tell you how to make it work because it should not work.

You should Open/Close connections outside the recursive part of your code.


EDIT

Proper code :

function recursiveQuery ( $ID, $numRows, $dbConnect ) {
  for($i=0;$i<=$numRows;$i++) {
    // [...]    

    for($x=0; $x < count($result); $x++){
      // [...]
      // here you don't want to return but just to execute :
      recursiveQuery( $r, $n, dbConnect);
    }                                             
  }
}

function wrapFunc($ID,$numRows) {
  $dbConnect = new MySqlConnect();
  $dbConnect->connect();
  // nothing is returned by your function (echos directly in the code)
  recursiveQuery ( $ID, $numRows, $dbConnect);
  $dbConnect->disconnect();
}

wrapFunc('6765','25');
Sign up to request clarification or add additional context in comments.

2 Comments

You are opening a new DB connection on each recursion. That's quite wrong.
@SergiuParaschiv that's is exactly the problem I am showing here.
0

Here I found the best answer for me: http://www.sitepoint.com/hierarchical-data-database/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.