Transferring data from one table to another

Discussion in 'PHP' started by egdcltd, Mar 11, 2007.

  1. #1
    I'm trying to get data from one table in a database and post it to another. The query to get the data is

    	$sql = 'SELECT * FROM '.ADR_LBUILDINGS_TABLE.' WHERE b_owner_id = '.$owner.' 
    
    	$result = $db->sql_query($sql);
    	if( !$result )
    		message_die(GENERAL_ERROR, 'Could not obtain buildings information', "", __LINE__, __FILE__, $sql);
    	$buildings = $db->sql_fetchrowset($result);
    PHP:
    From this, I want one particular column, b_id. There will be a variable number of entries in this column that match the SELECT criteria, including the chance of none being there.

    I then want to add all these entries to another table, in the character_activated_buildings column, replacing what's currently there with all the b_id from the query, in the format #b_id# for each b_id

    I can't quite seem to figure out how to do it.
     
    egdcltd, Mar 11, 2007 IP
  2. ruby

    ruby Well-Known Member

    Messages:
    1,854
    Likes Received:
    40
    Best Answers:
    1
    Trophy Points:
    125
    #2
    You want to move data from one table into another? Try insert into...

    
    insert into TABLE (VAL1) select VAL2 from TABLE2
    
    Code (markup):
     
    ruby, Mar 12, 2007 IP
  3. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It's actually a SET command that's required. What I can't figure out is how to add all the results from the query, with the proper formatting, to the second table.
     
    egdcltd, Mar 12, 2007 IP
  4. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #4
    To try and make it a bit clearer, here's what I'm attempting to do. First, this query is run
    $sql = 'SELECT * FROM '.ADR_LBUILDINGS_TABLE.' WHERE b_owner_id = '.$owner.'
    
        $result = $db->sql_query($sql);
        if( !$result )
            message_die(GENERAL_ERROR, 'Could not obtain buildings information', "", __LINE__, __FILE__, $sql);
        $buildings = $db->sql_fetchrowset($result); 
    PHP:
    From this, I want all instances of b_id that match the SELECT criteria.

    If (no instances)
    {$activated='';}
    else if b_id list is something like 10,13,22,27
    {activated = #10##13##22##27;}
    		$sql = "UPDATE ".ADR_CHARACTERS_TABLE." SET
    				character_activated_buildings = '$activated'
    				WHERE character_id = ".$owner;
    PHP:
    It's the bit in the middle I'm having trouble with
     
    egdcltd, Mar 12, 2007 IP
  5. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm pretty sure I need a for loop for the middle section.
     
    egdcltd, Mar 12, 2007 IP
  6. smo

    smo Well-Known Member

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #6
    smo, Mar 12, 2007 IP
  7. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    insert into t1 (select * from t2)
     
    jitesh, Mar 13, 2007 IP