How to connect php with IBM SQL Database using Bluemix?

泪湿孤枕 提交于 2019-12-13 06:09:31

问题


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/



来源:https://stackoverflow.com/questions/32745721/how-to-connect-php-with-ibm-sql-database-using-bluemix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!