vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   [MySQL] How to import a column from db1.table to db2.table (https://vborg.vbsupport.ru/showthread.php?t=266559)

kevin.kool 07-10-2011 04:09 AM

[MySQL] How to import a column from db1.table to db2.table
 
Hello,

I've just downgraded my forum from vb4 to vb3, and would like to keep the "thanked" amount from old db. I looked into the old db and notice that "thanked" is a column in "user" table.

So i would like to ask how can i move (or copy) that column from old database into the table of the new database?

Also, is there anyway to plus 1 value in the "userid" field (of that column)? Because of i've used ImpEx and it's created a new admin with userid = 1, then my old admin account has userid = 2.

Thank you.

Disasterpiece 07-10-2011 04:55 AM

You'd have to create a new column with the exact datatype like the original column (if that istn't already done) then you'd have to create a php script, that queries every user row, builds a new query with updated userid (+1) in the WHERE clause to apply the sql query.

Could look something like this:

Code:

SELECT userid, thankedamount FROM vb4_user
The function will return an array which you'll run through in a foreach loop, then you run a query for every user:

Code:

UPDATE vb4_user SET thankedamount = ".$thankedamount." WHERE userid = ".$userid+1
Depending on how good your knowledge in php is, this will be a quite simple task.

kevin.kool 07-10-2011 05:39 AM

I have ran this query in phpmyadmin and it's ok, but only for the user who has id=2, how to loop it?
Sorry because my php knowledge is bad.

This is the query that i have used:
Quote:

SELECT userid, post_thanks_thanked_times FROM vb4forum.user;
UPDATE vb3forum.user SET post_thanks_thanked_times = '.$post_thanks_thanked_times.' WHERE userid = '.$userid.+1';

Disasterpiece 07-10-2011 05:52 AM

it's no mysql query at all :)

there needs to be a php script built around this...
Can you tell me if the two vbulletin installations are stored in the same or in a different database?
And tell me how the exact name of the column is where the amount of thanks is stored.

kevin.kool 07-10-2011 06:02 AM

Two forums are store in 2 different databases. I wrote as 'vb4forum' and 'vb3forum' as above.
And the 'thanked amount' column name is post_thanks_thanked_times

Could you create a sample php page?

Disasterpiece 07-10-2011 06:24 AM

1 Attachment(s)
This is my old convert-youtube script. I just adjusted some values and added a 2nd database.

This only works if username and password are the same for the 2 databases.

open the file, enter your credentials at the top of the script. It works like
$db1 reads, $db2 writes. So $db1 is your old db to read from, $db2 is the one where you want the thanks transferred to.

Upload it into the root dir of your site (not forum root) so it can be accessed like "http://someurl.tld/transferthanks.php"
and run it.

didn't test it myself, if you're getting errors, report back. And I suggest to test it in a safe environment first.

kevin.kool 07-10-2011 06:53 AM

Thank for the file but when it's got error:

Quote:

Warning: Cannot use a scalar value as an array in C:\AppServ\www\transferthanks.php on line 34

Notice: Undefined variable: id in C:\AppServ\www\transferthanks.php on line 41
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1 (id: )
Line 34:
Code:

                $res['post_thanks_thanked_times'] = 0;
Line 41:
Code:

        else echo "Error: ".mysql_error()." (id: $id)<br />";

Disasterpiece 07-10-2011 06:57 AM

1 Attachment(s)
d'ouh, my bad.

Fixed

kevin.kool 07-10-2011 07:02 AM

Got this error:

Code:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1 (id: 1)
For the whole from id 1 to id 509 (i have 509 users) and nothing change in database.

Disasterpiece 07-10-2011 07:07 AM

huh weird... change line 42 from:

PHP Code:

    else echo "Error: ".mysql_error()." (id: $id)<br />"

to:
PHP Code:

    else echo "Error: ".mysql_error()." ($q) (id: $id)<br />"

And post what the first error is, thanks.

kevin.kool 07-10-2011 07:09 AM

Got this:

Quote:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1 (1) (id: 1)

Disasterpiece 07-10-2011 07:13 AM

Replace line 37 with this:

PHP Code:

    $q "UPDATE ".$tableprefix2."user SET post_thanks_thanked_times = ".$row['post_thanks_thanked_times']." WHERE userid = ".(int)($row['userid']+1); 


kevin.kool 07-10-2011 07:20 AM

It's Success. But it's look like it doesn't get thanked amount from database1. The thanked amount is from "write" db and being rewrite again with userid+1.

Disasterpiece 07-10-2011 10:52 AM

hum, well it should oO

did you re-check the database credentials?

kh99 07-10-2011 12:51 PM

Quote:

Originally Posted by kevin.kool (Post 2218831)
It's Success. But it's look like it doesn't get thanked amount from database1. The thanked amount is from "write" db and being rewrite again with userid+1.

[S]If you still have the old table in another database, you could try adding the database to the table names of the first query, like[/S]

Edit: never mind, I hadn't looked at the php file linked above where Disasterpiece had already handled the second database.

BTW, I think this could be done via query something like:
Code:

UPDATE db1.vb_user as old LEFT JOIN db2.vb4_user as new ON (old.userid = new.userid + 1)
SET new.thanked = old.thanked


(But I haven't tested that exact query). That of course assumes that you've already created a "thanked" column in the new db....and that the dbs are on the same server.

kevin.kool 07-10-2011 10:29 PM

Quote:

Originally Posted by Disasterpiece (Post 2218878)
hum, well it should oO

did you re-check the database credentials?

Yes, i've recheck the column after each run.

Quote:

Originally Posted by kh99 (Post 2218917)
[S]If you still have the old table in another database, you could try adding the database to the table names of the first query, like[/S]

Edit: never mind, I hadn't looked at the php file linked above where Disasterpiece had already handled the second database.

BTW, I think this could be done via query something like:
Code:

UPDATE db1.vb_user as old LEFT JOIN db2.vb4_user as new ON (old.userid = new.userid + 1)
SET new.thanked = old.thanked


(But I haven't tested that exact query). That of course assumes that you've already created a "thanked" column in the new db....and that the dbs are on the same server.

Could you rewrite the query with those extractly db name, table name and column name as i listed above?

kh99 07-10-2011 10:37 PM

I think this is it:

Code:

UPDATE vb4forum.user as old LEFT JOIN vb3forum.user as new ON (old.userid = new.userid + 1)
SET new.post_thanks_thanked_times = old.post_thanks_thanked_times


This sets the vb3forum database from the vb4forum database. I think what I posted above assumed that you were going from vb3 to vb4 (at least as far as the table prefixes were concerned). Speaking of which, if you have table prefixes you need to add them.

kevin.kool 07-10-2011 10:58 PM

I've down from vb4 to vb3, and there is no table prefix on 2 databases, i have full rights to modify them both.

I've just ran the query and there is an error:
Quote:

Error

SQL query:

UPDATE vb4forum.user AS old LEFT JOIN vb3forum.user AS new ON ( old.userid = new.userid +1 ) ;

MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

kh99 07-10-2011 11:13 PM

Hmm...well, both lines together are supposed to be all one query and it looks like you just have the first line there.

kevin.kool 07-10-2011 11:30 PM

yes, i've ran both of them together, but it shows up just 1 line for error.

kh99 07-10-2011 11:36 PM

The error looks like it's trying to execute only the first line. So I guess try this:

UPDATE vb4forum.user as old LEFT JOIN vb3forum.user as new ON (old.userid = new.userid + 1) SET new.post_thanks_thanked_times = old.post_thanks_thanked_times

kevin.kool 07-10-2011 11:41 PM

Okay, it's success :D kh99 you rock! Most of thanked amouths have right destination, i think i have to manual change few value.

Thank you all, both kh99 and Disasterpiece.


All times are GMT. The time now is 04:12 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.01166 seconds
  • Memory Usage 1,775KB
  • 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
  • (8)bbcode_code_printable
  • (3)bbcode_php_printable
  • (7)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (22)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