简体   繁体   English

MySql-从一个数据库更新到另一个数据库

[英]MySql - UPDATE from one database to another database

FYI: I have tried using other solutions that have been posted to resolve this query, but haven't had any success. 仅供参考:我曾尝试使用已发布的其他解决方案来解决此查询,但没有成功。 I have decided to use the code below as it seems the most logical to accomplish my task. 我决定使用以下代码,因为这似乎是完成我的任务的最合逻辑。 However, if there is a solution that is completely different, that is fine - as long as it works. 但是,如果有一个完全不同的解决方案,那很好-只要可行。

Within the same server, there are two databases with the same field names. 在同一服务器内,有两个具有相同字段名称的数据库。

Both DBs: 两个数据库:
ID, ID,
users, 用户,
pwds, 残疾人士,
emails 电子邮件

I am trying to grab all passwords from DB#2 (correct pwds) and move them to DB#1 (incorrect pwds) based upon each user's email address. 我试图根据每个用户的电子邮件地址从DB#2(正确的密码)中获取所有密码,并将其移至DB#1(错误的密码)中。 Unfortunately, I cannot use ID's as they are different for each user per database. 不幸的是,我无法使用ID,因为每个数据库的每个用户ID都不同。

The code below is not working. 下面的代码不起作用。 I am not receiving any errors - it's just not making the update. 我没有收到任何错误-只是没有进行更新。

Also, I realize that this type of coding has been deprecated. 另外,我意识到这种编码方式已被弃用。 If anyone has this solution using newer syntax, that would be great. 如果有人有使用较新语法的解决方案,那就太好了。

<?php

$dbhost1="localhost";
$dbname1="aaaaaa";
$dbuser1="bbbbbb";
$dbpass1="cccccc";

$dbhost2="localhost";
$dbname2="dddddd";
$dbuser2="eeeeee";
$dbpass2="ffffff";

$conn1 = mysql_connect($dbhost1, $dbuser1, $dbpass1);
$conn2 = mysql_connect($dbhost2, $dbuser2, $dbpass2, true);
if(! $conn1 || ! $conn2 )
{
  die('Could not connect to db1 or db2: ' . mysql_error());
}

mysql_select_db('db1', $conn1);
mysql_select_db('db2', $conn2);

$query = "UPDATE db1.users as t1, db2.users as t2 SET t1.pwds = t2.pwds WHERE t1.emails = t2.emails";

// LINE BELOW ADDED AFTER COMMENTS POSTED:
mysql_query($query, $conn1);

mysql_close($conn1);
mysql_close($conn2);

?>

$query构造行之后使用以下行:

mysql_query($query);

The folks that posted about the missing line of code regarding the mysql_query() were exactly correct. 关于mysql_query()的代码缺失行发布的人们是完全正确的。 However, I felt the need to continue on with this posting - to accentuate this information - for others also searching for this solution. 但是,我认为有必要继续发布该帖子-强调此信息-对于也在寻找此解决方案的其他人。

The actual problem was that both users needed to be assigned to both DBs and given ALL PRIVILEGES for both DBs. 实际的问题是,两个用户都需要分配给两个DB,并且都给了这两个DB ALL PRIVILEGES。 As long as I was accessing one DB at a time, everything was fine. 只要我一次访问一个数据库,一切都很好。 As soon as I coded two users and two DBs, the access error occurred. 一旦我编码了两个用户和两个DB,就发生了访问错误。

TIP: As was mentioned above also, add the extra lines of code to display the errors. 提示:如上所述,请添加额外的代码行以显示错误。 I've learned my lesson. 我学到了教训。 Thanks to all. 谢谢大家。

...and one last INSANELY important thing was the updated syntax for the actual query: ...还有最后一件非常重要的事情是实际查询的更新语法:

$query = "UPDATE db1.users INNER JOIN db2.users ON (db1.users.emails = db2.users.emails) SET db1.users.pwds = db2.users.pwds";

Missing the mysql_query() after the $query $query之后缺少mysql_query()

You just write like that 你只是这样写

if you want to update in $conn1 then 如果要在$ conn1中更新

mysql_query($query, $conn1);

And if you want to update in $conn2 then 如果要在$ conn2中更新

mysql_query($query, $conn2);

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

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