How to connect php with IBM SQL Database using Blu

2019-09-16 11:44发布

问题:

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.

回答1:

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, '', '');


回答2:

Please see the accepted answer in this post : https://developer.ibm.com/answers/questions/20036/how-to-compile-php-with-db2-support-in-bluemix/