简体   繁体   English

在BlueMix的同一php文件中连接另一个db2实例

[英]connecting another db2 instance in same php file in BlueMix

I connect to a db using this code 我使用此代码连接到数据库

//parse VCAP_SERVICES Environment variable //解析VCAP_SERVICES环境变量

$vcap_services = $_ENV["VCAP_SERVICES"];
$services_json = json_decode($vcap_services,true);
$sqldb = $services_json["sqldb"];
if (empty($sqldb)) {
    echo "No sqldb service instance is bound. Please bind a sqldb service instance";
    return;
}

//Get Credentials object (db,host,port,username,password)
$sqldb_config = $services_json["sqldb"][0]["credentials"];

$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=".
   $sqldb_config["db"].
   ";HOSTNAME=".
   $sqldb_config["host"].
   ";PORT=".
   $sqldb_config["port"].
   ";PROTOCOL=TCPIP;UID=".
   $sqldb_config["username"].
   ";PWD=".
   $sqldb_config["password"].
   ";";

then I do "my thing" and close the connection with 然后我做“我的事”并关闭与

db2_close($conn);

then when I try to connect to another instance of the db2 db by using the same connection code but changing the middle argument in this line: 然后,当我尝试使用相同的连接代码连接到db2 db的另一个实例时,但是更改了这一行的中间参数:

$sqldb_config = $services_json["sqldb"][0]["credentials"];

the connection does not work. 连接不起作用。 The two parts of code work just fine separately. 代码的两个部分可以很好地分开工作。 It's when I put the code in the same file in that order that it causes this error. 当我将代码按此顺序放入同一文件时,就会导致此错误。

Once you've closed a connection you have to open a new one (using the code you're already using) in order to access to the DB again. 关闭连接后,必须打开一个新连接(使用您已经在使用的代码)才能再次访问该数据库。 It is correct to close the connection after each 'logic' group of queries. 在每个“逻辑”查询组之后关闭连接是正确的。

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

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