Forum Moderators: coopster

Message Too Old, No Replies

copy table from another db on another server

table from another server

         

wawan

9:53 am on Feb 4, 2004 (gmt 0)

10+ Year Member



Hi, i am a newbie here.
I found some of the article very useful 4 some of my project (i am studying comp science).
Now, i am looking for help for my project with PHP-MySQL.
I am making a project that has 2 database (all with the same table). I have to put them on different server. So, i have 2 db on 2 server. All with the same table and same field, but with different data.
Then, the problem i faced, i want to show the data on a table, added it (union) with data from same table on another server.

If the databases are in the same server, i can use simple script:

create temporary table table_union select * from database1.table;
insert into table_union select from * from database2.table;

then use some code select to show the content of temporary table table_union (very fast because temporary table created in memory)

but, now i have to deal with 2 database in 2 diff. server. Is there any chance that i can use some same code like the one use temporary table? i realise that one of the solution maybe is by using the mysql_dump. but, it means i have to import and make table on the server.
i want to find the solution for the problem by connecting directly to diff. db on diff. server on one script. so, the solution have to deal with the connection.

Can anyone help me? THanks for support.

jatar_k

8:48 pm on Feb 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld wawan,

Looks to me like you would have to open 2 connections, select from db1 then select from db2 and use php to collate the data.

wawan

11:55 pm on Feb 4, 2004 (gmt 0)

10+ Year Member



Yes, Jatar.
I try to find the solution. i crawl over some of discussion topic in this site and some other site. But, i didn't find the useful one.

What i really need is the solution in php, which connect me to 2 db. BTW, how to colate the data in php? do i have to save data from db1 to some variable first and combine it with data from db2?

Can u give me some example?

wawan

12:07 am on Feb 5, 2004 (gmt 0)

10+ Year Member



Mmmmm... How to use mysql_dump function in php script?

jatar_k

1:27 am on Feb 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I guess it really depends on how many records we are talking about. Having a script that connects to 2 seperate servers for a combined list of data doesn't seem like the best way to do things. Though it screams for a different model this will work.

What you need to do is connect using mysql_connect [ca3.php.net] to one server, pull the data you need into an array, possibly, and then do the same on the other server.

When you connect to the seecond server you could close the original connection and connect to the new one, keeping it as logical as possible. When you grab the results from the second server you could use the already loaded array to add too.

This depends utterly on whether the data matches or you need only results that do not appear in the array already or you just need to add data to the existing array elements. I couldn't really tell you what exactly you need to do.

That is the rough outline of the process. Start by connecting to the first server and loading an array. Once that is working analyze the differences in data on the second and see what is the best way to merge your results. Then start on the merging process.

That should probably get you rolling, after all, it's your degree, not mine. ;)

wawan

8:10 am on Feb 5, 2004 (gmt 0)

10+ Year Member



can u help me to store the data in some array? what is the limit?

coopster

4:01 pm on Feb 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



jatar_k has already provided direction in this thread and although this post won't be off-topic, it does interrupt the flow that's been started, so please forgive me but I just wanted to explain why we cannot query databases on different servers in a single statement as wawan is asking in msg#1.

You can successfully use multiple databases over a local or a remote connection in the same statement when they share the same resource link_identifier:


PHP
mysql_query() [php.net]sends a query to the currently active database on the server that's associated with the specified link identifier. If link_identifier isn't specified, the last opened link is assumed. If no link is open, the function tries to establish a link as if mysql_connect() [php.net] was called with no arguments, and use it.

As you should know, each MySQL connection is associated to a link identifier. That happens because

mysql_connect
returns the resource link identifier on success, or
FALSE
on failure.

Therefore if we establish two connections, each to a different server, and our query statement is only capable of using one connection via it's resource link identifier, we cannot use the databases on the other link.

jatar_k

7:02 pm on Feb 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Maybe take a look at this and see if that answers some of your questions
Basics of extracting data from MySQL using PHP [webmasterworld.com]

In msg Step 5 - you could put the data into an array instead of displaying it by changing the display loop

while ($row = mysql_fetch_array($query)) {
echo "<p>",$row['id'],": ",$row['manufacturer'];
}

to

while ($row = mysql_fetch_array($query)) {
$myarr[] = array($row['id'],$row['manufacturer']);
}