Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-16-2012, 11:44 PM   PM User | #1
WingTsun
New Coder

 
Join Date: Jan 2012
Location: Latvia
Posts: 65
Thanks: 8
Thanked 2 Times in 2 Posts
WingTsun is an unknown quantity at this point
Thumbs up [PHP\MySql] Transfer data between two tables..

SOLVED!
Solution posted below..

Hello guys..!

I'm having some trouble transferring some data dynamicly from one table of the database to another.

The Database and Table Structure is IDENTICAL. But the data, and entry KEYS are DIFFERENT.

What I need to do is to transfer data from `DB_1.DATA_TABLE` to `DB_2.DATA_TABLE`, but the Entry KEY must be specific and I want to create that Key by my own.

I need to do this loop for about 20 different tables, and every table have different structure.. Only the Source\Target DBs ar Identic.

I am doing this in a loop, I am trying like this now, but cant get it working:
Note: All the Variables are passed correctly, I just need a correct syntax to get this working.


PHP Code:
while($TransferData mysql_fetch_array($SqlSource)) {
                                                        
    
KOV::reconnect();                            
    
mysql_query("INSERT INTO `wow_characters_target`.`account_data` (`accountId`) VALUES ('" .$SqlTarget['accountId']. "') SELECT * FROM `wow_characters_source`.`account_data` WHERE `accountId` = '" .$TransferData['accountId']. "'");
    } 
I made a solution my self, and the problem is solved. Thank you!

PHP Code:

# This code here is located in another loop..

$SqlGetStructure    mysql_query("DESCRIBE `" .$table_name"`");
$ConstructedData    = array();        
$ConstructedColumns '';
$ConstructedValues  '';

# Make an Array with the data needed to insert into the Target Table..

    
while($DescribeData mysql_fetch_array($SqlGetStructure)) {
                                
        if(
$DescribeData['Field'] != 'accountId'){ 
                                    
            
$ConstructedData[$DescribeData['Field']] = $TransferData[$DescribeData['Field']];
        }
    }

# Set the Unique data..

        
$ConstructedData['guid']      = $SqlLastId['guid']+1;
        
$ConstructedData['account'] = $target_key;

# Construct the Structure and Values to be Mysql Query Ready..

    
foreach($ConstructedData as $key => $data) {
                                                
        
$ConstructedColumns .= '`' .$key'`,';
        
$ConstructedValues  .= "'" .addslashes($data). "',";
    }

        
$ConstructedColumns substr_replace($ConstructedColumns ,"",-1);    
        
$ConstructedValues  substr_replace($ConstructedValues ,"",-1);

# Execute the Query

         
$Execute mysql_query("INSERT INTO `characters`(" .$ConstructedColumns") VALUES (" .$ConstructedValues")"); 
Thank you for your help,
Ralph!
__________________
"Be formless... shapeless, like water. Now you put water into a cup, it becomes the cup. You pour water into a bottle; it becomes the bottle. You put water into a teapot; it becomes the teapot. Now water can flow, or creep or drip or crash! Be water, my friend..." by Bruce Lee

Last edited by WingTsun; 10-17-2012 at 10:40 PM..
WingTsun is offline   Reply With Quote
Old 10-17-2012, 12:30 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
INSERT. . . SELECT commands do not allow you to specify values for individual fields. You need to specify all the fields and then apply any constant data to the select. So no select *. See the api documentation for the insert. . . select here: http://dev.mysql.com/doc/refman/5.1/...rt-select.html
I don't know why you need to bother with this though tbh. The key should never need to change.
Fou-Lu is offline   Reply With Quote
Old 10-17-2012, 12:49 AM   PM User | #3
WingTsun
New Coder

 
Join Date: Jan 2012
Location: Latvia
Posts: 65
Thanks: 8
Thanked 2 Times in 2 Posts
WingTsun is an unknown quantity at this point
So, I will need to make an individual query for every table I want to merge?
__________________
"Be formless... shapeless, like water. Now you put water into a cup, it becomes the cup. You pour water into a bottle; it becomes the bottle. You put water into a teapot; it becomes the teapot. Now water can flow, or creep or drip or crash! Be water, my friend..." by Bruce Lee
WingTsun is offline   Reply With Quote
Old 10-17-2012, 02:36 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I don't understand your question. You'd need to create a new query for anything that differs, yes, but that would be required as something has changed anyways (fields, tables, whatever). In the above case, you'll likely need to issue an insert on each record anyways (use a combined insert or a batch using a prepared statement and bind them) unless there is an automated fashion in which a new key can be generated. If there is a way to automate the key such as UUID or autoincrement for a primary key, then you can simply issue a single INSERT. . .SELECT call.
Fou-Lu is offline   Reply With Quote
Old 10-17-2012, 10:41 PM   PM User | #5
WingTsun
New Coder

 
Join Date: Jan 2012
Location: Latvia
Posts: 65
Thanks: 8
Thanked 2 Times in 2 Posts
WingTsun is an unknown quantity at this point
The problem is solved now. Thank you! I posted the solution in the Original Post, and set the status as "resolved".
__________________
"Be formless... shapeless, like water. Now you put water into a cup, it becomes the cup. You pour water into a bottle; it becomes the bottle. You put water into a teapot; it becomes the teapot. Now water can flow, or creep or drip or crash! Be water, my friend..." by Bruce Lee
WingTsun is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:18 AM.


Advertisement
Log in to turn off these ads.