PDA

View Full Version : Help with MySQL query


glennybee
03-25-2013, 08:27 PM
Hi folks,

I was wondering if anyone could help me with a query please?

I have two mysql databases. The information is contained within the user table. db1 has information in 3 columns which I want to copy to the same columns in db2. And I want to do it with for all users on the forum.

Any help would be great. :)

Thanks! :)

glennybee
03-30-2013, 08:39 PM
I've managed to put this together but it's not working...


<?php
$dba = mysql_connect('localhost', 'username_vbtest', 'password') or die(mysql_error());
$sql = "
INSERT INTO username_vbtest.user (
post_thanks_user_amount,
post_thanks_thanked_posts,
post_thanks_thanked_times
)
SELECT (
username_test.user.post_thanks_user_amount,
username_test.user.post_thanks_thanked_posts,
username_test.user.post_thanks_thanked_times
)
FROM username_test.user
";
$result = mysql_query($sql) or die(mysql_error());

mysql_close($dba);


I'd really appreciate any help with this. Thanks! :)

'vbtest' is the destination database and 'test' is the source database.

kh99
03-30-2013, 09:24 PM
What happens? Do you get an error?


'vbtest' is the destination database and 'test' is the source database.

You have username_test and username_vbtest in the code.

glennybee
03-31-2013, 08:11 PM
So I need to connect to two databases at once then?

Both databases have the same username and password if that makes it easier.

So am I overcomplicating this?

Would this also work?


$dbh1 = mysql_connect($localhost, $username, $password);
$dbh2 = mysql_connect($localhost, $username, $password, true);

mysql_select_db('database1', $dbh1);
mysql_select_db('database2', $dbh2);

USE `database2`;
INSERT INTO `database1`.`new_table`(`column1`,`column2`,`colum n3`)
SELECT `old_table`.`column2`, `old_table`.`column7`, `old_table`.`column5`
FROM `old_table`

kh99
03-31-2013, 08:23 PM
I was just pointing out the code you posted didn't look like it was using the names of the databases you posted. As far as I know, if the databases are on the same server and the database user you're logging in as has the correct permissions on both, then you should be able to do it with something similar to what you originally posted. I'm not sure if what you posted is exaclty the right syntax, I'd have to try it.