SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Select Query

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    PHP Code:
    $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); 

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    Hi,

    This should work:

    PHP Code:
    $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:

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

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the reply, but it didn't work somehow, while I found another way around.
    PHP Code:

    $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); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •