Reputation: 3668
I wrongly designed my application to have one database to each user. each user had 3 similar tables. I now want to have one database and 3 tables only; where i will use the database name in the old databases as a reference in the new system. There was another database called "users" in the old database that stored the database names. I'm done with the schema design of the new database and now left with the migration.
The trick here is that I have 3 db connections. I first connect to the users database and userinfo table, pick up the database_name in a loop, use it to connect each old db and further connect to personal, accounts and games table.
After picking up the tables, i will like to populate/join it with the new Database (DATA_ONE) and the tables whiles i append the old database_name to the new tables. Any help on this or is there a better way to do this? Thanks much
<?php
$dbhost = "localhost";
$dbname = "users";
$dbuser = "root";
$dbpass = "*****";
$conn1 = mysql_connect($dbhost, $dbuser, $dbpass, TRUE) or die("MySQL Error: " . mysql_error());
$conn2 = mysql_connect($dbhost, $dbuser, $dbpass, TRUE) or die("MySQL Error: " . mysql_error());
$conn3 = mysql_connect($dbhost, $dbuser, $dbpass, TRUE) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname,$conn1) or die("MySQL Error: " . mysql_error());
$query = "SELECT database_name FROM userinfo";
$result1 = mysql_query($query,$conn1);
while($row = mysql_fetch_array($result1, MYSQL_ASSOC))
{
$database_name =$row['database_name'];
$conn2 = mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
$db = mysql_select_db($database_name ,$conn2) ;
// now, pick personal, accounts, games tables of user and populate new database and tables.
// this is where i need help.
}
?>
Upvotes: 4
Views: 259
Reputation: 48367
Why do you think you need 3 seperate database connections with the same credentials? In any mysql query you can reference tables from any database on the same instance by prefixing the table name with the database name (and a . in between). I'd suggest not using 'database_name' as an attribute name:
$databases=get_mysql('SELECT DISTINCT database FROM users.userinfo ORDER BY database');
$count_of_users=count($databases);
foreach ($databases as $user_offset=>$user) {
$uqry="INSERT INTO data_one.personal
(user, id, address, password)
SELECT '$user'
, (id*$count_of_users*$user_offset)
, address
, password
FROM ${user}.personal";
...
(assuming that 'id' is an autoincrement value which may be referenced elsewhere).
Upvotes: 1