简体   繁体   English

PHP-连接到多个MySQL数据库

[英]PHP - Connect to Multiple MySQL Databases

So I have a PHP page that connects first to my database and does a bunch of stuff using the information from there. 因此,我有一个PHP页面,该页面首先连接到我的数据库,并使用那里的信息来做很多工作。 Now I want to connect to another database within the same PHP page and access data from there and insert the information into my original database. 现在,我想连接到同一PHP页面中的另一个数据库,并从那里访问数据并将信息插入到我的原始数据库中。

The code: 编码:

<?php

session_start();

include ("account.php");
include ("connect.php");

....
do stuff here
....

include ("account2.php");
include ("connect2.php");

...
$thing = "SELECT abc, efg, hij FROM table ORDER BY RAND() LIMIT 1" ;
$thing = mysql_query($thing);
echo "$thing";
....

....
insert information into my database
(From account.php & connect.php files)
....

?>

Everything shows up except for $thing. 一切都出现了,除了$ thing。 It says, "Invalid query: Query was empty" but I know the query I used works because when I ran it in the account2 database, I got the results I wanted. 它说:“无效查询:查询为空”,但我知道我使用的查询有效,因为当我在account2数据库中运行该查询时,得到了想要的结果。 Is there something wrong with my logic or is it something else? 我的逻辑有问题吗?还是其他问题?

You are not mentioning database connection link while executing the query. 您在执行查询时没有提及数据库连接链接。 May be your second database connection is defined after the 1st one in connection.php file. 可能是您的第二个数据库连接是在connection.php文件中的第一个数据库之后定义的。 So interpreter is considering 2nd connection while executing the query. 因此,解释器在执行查询时正在考虑第二连接。

Define the connection link with query, 使用查询定义连接链接,

$thing = mysql_query($thing,$conn); //$conn is your mysql_connect

You can still use the mysql_connect or similar mysql_ functions (procedural way) But all these are now deprecated. 您仍然可以使用mysql_connect或类似的mysql_函数(过程方式),但是现在不推荐使用所有这些方法。 You should use mysqli_ (mysql improved) functions or go with the object oriented way. 您应该使用mysqli_(改进了mysql)功能或采用面向对象的方式。

$conn1 = new mysqli(SERVER1,DB_USER1,DB_PASS1,DB1);
$conn2 = new mysqli(SERVER2,DB_USER2,DB_PASS2,DB2);

if($conn1 ->connect_errno > 0 || $conn2 ->connect_errno > 0){
    die('Unable to connect to database server.');
}

Now while executing your query, 现在,在执行查询时,

$query = "SELECT abc, efg, hij FROM table ORDER BY RAND() LIMIT 1";
$thing = $conn1 -> query($query); //if it's second connection query user $conn2

Many web applications benefit from making persistent connections to database servers. 许多Web应用程序受益于与数据库服务器的持久连接。 Persistent connections are not closed at the end of the script, but are cached and re-used when another script requests a connection using the same credentials. 持久连接不会在脚本结尾处关闭,而是在另一个脚本使用相同凭据请求连接时被缓存并重新使用。 The persistent connection cache allows you to avoid the overhead of establishing a new connection every time a script needs to talk to a database, resulting in a faster web application. 持久连接缓存使您避免每次脚本需要与数据库进行通信时建立新连接的开销,从而加快了Web应用程序的速度。

For your case, i would make 2 persistent connections, store each in it's own variable and then use one whenever i need to. 对于您的情况,我将建立2个持久连接,将每个连接存储在自己的变量中,然后在需要时使用一个。 Check it out using the PDO class. 使用PDO类进行检查。

//Connection 1
$dbh1 = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
    PDO::ATTR_PERSISTENT => true
));

.
.
.

//Connection 2
$dbh2 = new PDO('mysql:host=localhost;dbname=test2', $user, $pass, array(
    PDO::ATTR_PERSISTENT => true
));

After you finish you first database operation, then you can close the connection using 完成第一个数据库操作后,可以使用以下命令关闭连接

mysql_close($c) //$c = connection

And again start for next database operation. 并再次开始下一个数据库操作。

private static function _connectDB1()
{
//give hostname, dbusername, dbpassword
$con1 = mysql_connect("localhost", "root", "rootpass");
$db1 = mysql_select_db("dbone", $con1);
}
private static function _connectDB2()
{
//if you are using different db with different credentials
//you can give here like this
// mysql_connect("mynewhost", "mynewusername", "mynewpassword")
$con2 = mysql_connect("localhost", "root", "rootpass");
$db2 = mysql_select_db("dbtwo", $con2);
}

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

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