Select Query

I need help to figure out how to pass $child from first query to where clause in the final select query. This way it’s only pass the last id out of a list of id(s) from TABLE.


$child_query = tep_db_query("select id, parent_id from " . TABLE . " where parent_id = '" . (int)$parent . "'");
    $row = 0;
    while ($child_id = tep_db_fetch_array($child_query)) {
      $row++;
	  $child = $child_id['id'];
}

$final = "select id, parent_id from " . TABLE . " where id = '" . $child . "'";
$final_query = tep_db_query($final);

Hi,

This should work:

$child_query = tep_db_query("select id, parent_id from " . TABLE . " where parent_id = '" . (int)$parent . "'"); 
    $row = 0;
    $firstChild = 0;
    while ($child_id = tep_db_fetch_array($child_query)) {
      if ($row == 0){$firstChild = $child_id['id'];}
      $row++; 
      $child = $child_id['id']; 
} 

$final = "select id, parent_id from " . TABLE . " where id = '" . $firstChild . "'"; 
$final_query = tep_db_query($final);

There’s probably a better way to do this, but I’m not at the PC right now, so cannot test.

HTH

Edit:
If tep_db_fetch_array works like mysql_fetch_array (i.e. it just returns the next row of the result) and there is no other reason for having a while loop, you could probably do something like this:

$child_id = tep_db_fetch_array($child_query)
$final = "select id, parent_id from " . TABLE . " where id = '" . $child_id['id'] . "'";

Thank you for the reply, but it didn’t work somehow, while I found another way around.



$child_query = tep_db_query("select id, parent_id from " . TABLE . " where parent_id = '" . (int)$parent . "'");
    while ($child_id = tep_db_fetch_array($child_query)) {
	  $array[] = $child_id['categories_id'];
	}

$final = "select id, parent_id from " . TABLE . " where id  IN (" . implode( ',', $array ) . ")";
$final_query = tep_db_query($final);