简体   繁体   English

如何使用Bluemix将php与IBM SQL Database连接?

[英]How to connect php with IBM SQL Database using Bluemix?

Suppose, i am connect to mysql database using mysql_coni(), so that now i am try to connect IBM SQL Database which provide by IBM Bluemix. 假设,我使用mysql_coni()连接到mysql数据库,因此现在我尝试连接由IBM Bluemix提供的IBM SQL数据库。

Which function or which correct way used to connect PHP With IBM SQL Database within bluemix. 在bluemix中使用哪种函数或哪种正确方法将PHP与IBM SQL数据库连接。

To connect to the SQLDB service in Bluemix you can use db2_connect. 要连接到Bluemix中的SQLDB服务,您可以使用db2_connect。 You should use a connection string instead of separate database/username/pw/etc. 您应该使用连接字符串,而不是单独的数据库/用户名/ pw / etc。 parameters because it is a remote DB service 参数,因为它是远程数据库服务

Following an example of parsing VCAP_SERVICES to connect to the SQLDB service in PHP: 以下是解析VCAP_SERVICES以连接到PHP中的SQLDB服务的示例:

# Decode JSON for DB connection parameters
$services_json = json_decode($json,true);
$sqldb = $services_json["sqldb"];
if (empty($sqldb)) {
    echo "No sqldb service instance bound. Please bind a sqldb service instance before";
    return;
}

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

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

// connect to database
$conn = db2_connect($conn_string, '', '');

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

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