How to connect php with IBM SQL Database using Blu

2019-09-16 12:02发布

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.

Which function or which correct way used to connect PHP With IBM SQL Database within bluemix.

2条回答
Summer. ? 凉城
2楼-- · 2019-09-16 12:13

To connect to the SQLDB service in Bluemix you can use db2_connect. You should use a connection string instead of separate database/username/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:

# 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, '', '');
查看更多
登录 后发表回答