vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Help with MySQL query (https://vborg.vbsupport.ru/showthread.php?t=296514)

glennybee 03-25-2013 08:27 PM

Help with MySQL query
 
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...

Code:

<?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?

Quote:

Originally Posted by glennybee (Post 2413177)
'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?

Code:

$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`,`column3`)
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.


All times are GMT. The time now is 10:09 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01164 seconds
  • Memory Usage 1,717KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete