简体   繁体   English

php / mysql数据库重新设计和迁移:将数据库更改为表

[英]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. 每个用户有3个类似的表。 I now want to have one database and 3 tables only; 我现在想要一个数据库和3个表; 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. 旧数据库中存在另一个名为“users”的数据库,用于存储数据库名称。 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. 这里的诀窍是我有3个数据库连接。 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. 我首先连接到users数据库和userinfo表,在循环中获取database_name,使用它连接每个旧数据库并进一步连接到个人,帐户和游戏表。

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. 拿起表后,我想用新数据库(DATA_ONE)和表格填充/加入它,而我将旧的database_name附加到新表。 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.   
  }

 ?>

Why do you think you need 3 seperate database connections with the same credentials? 为什么您认为需要具有相同凭据的3个单独的数据库连接? 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). 在任何mysql查询中,您可以通过在表名前加上数据库名称(以及中间的。)来引用同一实例上任何数据库中的表。 I'd suggest not using 'database_name' as an attribute name: 我建议不要使用'database_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). (假设'id'是一个自动增量值,可以在其他地方引用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM