/    Sign up×
Community /Pin to ProfileBookmark

Data from one database and insert into a new database

Can someone show me a php script that will query a db then place the results into a different database.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 23.2010 — If the same database connection [i]and[/i] the same database user can access both databases, then it can be done completely via SQL, probably a single query.
Copy linkTweet thisAlerts:
@porsche21authorSep 23.2010 — Ok it does use the same db connection and user. Could you give an example of what the code should look like? What if there were a different connection and user, what would I do then?
Copy linkTweet thisAlerts:
@NogDogSep 23.2010 — If the same connection/user, you should be able to do something like:
[code=php]
$connx = mysql_connect('localhost', 'dbuser', 'dppassword') or die(mysql_error());
$sql = "
INSERT INTO database2.table_name (fld1, fld2, fld3)
SELECT fld1, fld2, fld3 FROM database1.table_name
";
$result = mysql_query($sql) or die(mysql_error());
[/code]

If separate connections/users are required, then you'd either have to query the first DB, save the results into an array, then use that array for inserting into the 2nd DB; or perhaps you could use the DBMS command line functions to create a SQL file, then load that file into the second DB.
Copy linkTweet thisAlerts:
@porsche21authorSep 23.2010 — Ok thanks, that makes total sense.
×

Success!

Help @porsche21 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...