php/mysql database redesign and migration: changing databases to tables 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

&lt;?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.   
    }

?&gt;
Instead of Migration to another System, try doing it in place, then mysqldump DATA_ONE and import that mysqldump into the new server.
First, let's create DATA_ONE
DROP DATABASE IF EXISTS DATA_ONE;
CREATE DATABASE DATA_ONE;

Next, Create DATA_ONE tables using the template from database aa123
CREATE TABLE DATA_ONE.personal LIKE aa123.personal;
CREATE TABLE DATA_ONE.accounts LIKE aa123.accounts;
CREATE TABLE DATA_ONE.games    LIKE aa123.games;

Now, Create a UserInfo Table
CREATE TABLE DATA_ONE.UserInfo
(
    userid INT NOT NULL AUTO_INCREMENT,
    username VARCHAR(32),
    last_update DATETIME NOT NULL,
    PRIMARY KEY (userid),
    KEY (username)
);
INSERT INTO DATA_ONE.UserInfo (username,last_update)
SELECT database_name,last_update FROM users.userinfo;

Then, add the database_name column (and index it) to the DATA_ONE tables
ALTER TABLE DATA_ONE.personal ADD username VARCHAR(32);
ALTER TABLE DATA_ONE.personal ADD userid INT;
ALTER TABLE DATA_ONE.accounts ADD userid INT;
ALTER TABLE DATA_ONE.games    ADD userid INT;
ALTER TABLE DATA_ONE.personal ADD INDEX (username);
ALTER TABLE DATA_ONE.personal ADD INDEX (userid);
ALTER TABLE DATA_ONE.accounts ADD INDEX (userid);
ALTER TABLE DATA_ONE.games    ADD INDEX (userid);

OK, that makes the new DATA_ONE database.
Now comes the adventurous part: Loading all those tables into DATA_ONE.
Run these three queries to formulate the SQL needed for migration
SELECT CONCAT('INSERT INTO DATA_ONE.personal (address,password,username,userid) SELECT address,password,'''username''',',userid,' FROM ',username,'.personal;') FROM DATA_ONE.UserInfo;
SELECT CONCAT('INSERT INTO DATA_ONE.accounts (amount,bank,userid) SELECT amount,bank,',userid,' FROM ',username,'.accounts;') FROM DATA_ONE.UserInfo;
SELECT CONCAT('INSERT INTO DATA_ONE.games (game_name,userid) SELECT game_name,',userid,' FROM ',username,'.games;') FROM DATA_ONE.UserInfo;

Here is everything mentioned above shell-scripted and executed:
SQLFILE=/root/MakeDATA_ONE_Database.sql
echo &quot;DROP DATABASE IF EXISTS DATA_ONE;&quot; &gt; ${SQLFILE}
echo &quot;CREATE DATABASE DATA_ONE;&quot; &gt;&gt; ${SQLFILE}
echo &quot;CREATE TABLE DATA_ONE.personal LIKE aa123.personal;&quot; &gt;&gt; ${SQLFILE}
echo &quot;CREATE TABLE DATA_ONE.accounts LIKE aa123.accounts;&quot; &gt;&gt; ${SQLFILE}
echo &quot;CREATE TABLE DATA_ONE.games    LIKE aa123.games;&quot; &gt;&gt; ${SQLFILE}
echo &quot;CREATE TABLE DATA_ONE.UserInfo (userid INT NOT NULL AUTO_INCREMENT,username VARCHAR(32),last_update DATETIME NOT NULL,PRIMARY KEY (userid),KEY (username));&quot; &gt;&gt; ${SQLFILE}
echo &quot;INSERT INTO DATA_ONE.UserInfo (username,last_update) SELECT database_name,last_update FROM users.userinfo;&quot; &gt;&gt; ${SQLFILE}
echo &quot;ALTER TABLE DATA_ONE.personal ADD username VARCHAR(32);&quot; &gt;&gt; ${SQLFILE}
echo &quot;ALTER TABLE DATA_ONE.personal ADD userid INT;&quot; &gt;&gt; ${SQLFILE}
echo &quot;ALTER TABLE DATA_ONE.accounts ADD userid INT;&quot; &gt;&gt; ${SQLFILE}
echo &quot;ALTER TABLE DATA_ONE.games    ADD userid INT;&quot; &gt;&gt; ${SQLFILE}
echo &quot;ALTER TABLE DATA_ONE.personal ADD INDEX (username);&quot; &gt;&gt; ${SQLFILE}
echo &quot;ALTER TABLE DATA_ONE.personal ADD INDEX (userid);&quot; &gt;&gt; ${SQLFILE}
echo &quot;ALTER TABLE DATA_ONE.accounts ADD INDEX (userid);&quot; &gt;&gt; ${SQLFILE}
echo &quot;ALTER TABLE DATA_ONE.games    ADD INDEX (userid);&quot; &gt;&gt; ${SQLFILE}
mysql -u... -p... -ANe&quot;SELECT CONCAT('INSERT INTO DATA_ONE.personal (address,password,username,userid) SELECT address,password,'''username''',',userid,' FROM ',username,'.personal;') FROM DATA_ONE.UserInfo;&quot; &gt;&gt; ${SQLFILE}
mysql -u... -p... -ANe&quot;SELECT CONCAT('INSERT INTO DATA_ONE.accounts (amount,bank,userid) SELECT amount,bank,',userid,' FROM ',username,'.accounts;') FROM DATA_ONE.UserInfo;&quot; &gt;&gt; ${SQLFILE}
mysql -u... -p... -ANe&quot;SELECT CONCAT('INSERT INTO DATA_ONE.games (game_name,userid) SELECT game_name,',userid,' FROM ',username,'.games;') FROM DATA_ONE.UserInfo;&quot; &gt;&gt; ${SQLFILE}
mysql -u... -p... -A &lt; ${SQLFILE}

Now open mysql-workbench or phpmyadmin or whatever you browse data with and look through the DATA_ONE database.
If you are satisfied with what is there, just do this:
mysqldump -u... -p... --databases DATA_ONE &gt; /root/DATA_ONE.sql

Move the DATA_ONE.sql file over to the new server and execute the script.
That's it. Pure SQL solution. No PHP.  If anything does not work, it is probably because I do not know the layout and current contents of the personal, account, &amp; games tables and made assumptions on data. Please post the following in your question:
SHOW CREATE TABLE aa123.personal\G
SHOW CREATE TABLE aa123.accounts\G
SHOW CREATE TABLE aa123.games\G

Give it a Try !!!
UPDATE 2012-05-23 13:57 EDT
Oh, you have 2500 databases? My above solution should still work.
I do have an alternative suggestion: Try to mysqldump all databases into individual files.
Here is a shell script that will dump all 2500 databases into separate files and gzipped
MYSQL_CONN=&quot;-uusername -ppassword&quot;
SQLSTMT=&quot;SELECT DISTINCT table_schema FROM information_schema.tables&quot;
SQLSTMT=&quot;${SQLSTMT} WHERE table_schema NOT IN&quot;
SQLSTMT=&quot;${SQLSTMT} ('information_schema','mysql','performance_schema')&quot;
mysql ${MYSQL_CONN} -ANe&quot;${SQLSTMT}&quot; &gt; /tmp/ListOfDatabases.txt
MYSQL_CONN=&quot;-uusername -ppassword&quot;
MYSQLDUMP_OPTIONS=&quot;--hex-blob --routines --triggers&quot;
COMMIT_COUNT=0
COMMIT_LIMIT=50
for DB in `cat /tmp/ListOfDatabases.txt`
do
    mysqldump ${MYSQL_CONN} ${MYSQLDUMP_OPTIONS} --databases ${DB} | gzip &gt; ${DB}.sql.gz &amp;
    (( COMMIT_COUNT++ ))
    if [ ${COMMIT_COUNT} -eq ${COMMIT_LIMIT} ]
    then
        COMMIT_COUNT=0
        wait
    fi
done
if [ ${COMMIT_COUNT} -gt 0 ]
then
    wait
fi

Once you execute this, all databases will be mysqldump'd. For example, the database aa123 will be dumped and gzipped to aa123.sql.gz. This and all the other 2499 mysqldumps can be copied over to the new DB server. Place the dumps in /root/mysqldata on the new DB server. Once they are on the new DB server, you can then unzip all files like this:
cd /root/mysqldata
for X in `ls *.sql.gz` ; do echo ${X} ; gzip -d ${X} ; done

OK this will unzip all the files.
Now, run this to load the SQL scripts into mysql on the new server
cd /root/mysqldata
for X in `ls *.sql` ; do echo ${X} ; mysql -u... -p... -A &lt; ${X} ; done

That's it. No PHP involved. All iteration is handled in the shell script I provided. You can then combine them in the new DB Server as DATA_ONE like I mentioned before.
Give it a Try !!!