如何在PHP Web应用程序中使用多个数据库? [英] How to use multiple database in a PHP web application?

查看:68
本文介绍了如何在PHP Web应用程序中使用多个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个PHP Web应用程序,其中使用MySQL作为数据库服务器,我想将某些表从一个数据库备份到另一个数据库(其中包含该表). 我创建了两个不同的连接,但表未更新.

I am making a PHP web Application in which i am using MySQL as database server, i want to make backup of some tables from one database to another database(with that tables in it). i have created two different connection, but the table is not updated.

$dbcon1 = mysql_connect(DB_SERVER,DB_USER,DB_PASSWORD) or die(mysql_error());
$dbase1 = mysql_select_db(TEMP_DB_NAME,$dbcon)or die(mysql_error());

$query1=mysql_query("SELECT * FROM emp");

while($row = mysql_fetch_array($query1, MYSQL_NUM))
{
    $dbcon2 = mysql_connect(DB_SERVER,DB_USER,DB_PASSWORD) or die(mysql_error());
    $dbase2 = mysql_select_db(TEMP_DB_NAME2,$dbcon)or die(mysql_error());

    mysql_query("INSERT INTO backup_emp VALUES(null,'$row[1]',$row[2])");
    mysql_close($dbcon2);
}

上面的代码从第一个数据库中获取emp的数据,并将其更新数据到另一个数据库的另一个backup_emp表中. 代码无法正常工作,还有其他方法可以执行此操作吗?请帮助.

the code above is taking the data of emp from first database, and updataing it into another backup_emp table of another database. the code is not working properly, is there any other way of doing this...please help.

推荐答案

如果两个数据库都在同一服务器上,并且表具有相同的架构,则应改为使用以下查询:

If both databases they are on the same server, and the tables have the same schema, then you should use this query instead:

mysql_query("INSERT INTO database2.backup_emp SELECT * FROM database1.emp");

这样,您无需担心多个数据库连接.

This way you don't need to worry about multiple database connections.

这篇关于如何在PHP Web应用程序中使用多个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆