Sunny
Sunny

Reputation: 11

How to connect to a SQL DB with PHP in Bluemix

I am binding a service to my database and I found the following code online:

$services_json = json_decode($json,true);   <br/>
$sqldb = $services_json["sqldb"];    <br/>
if (empty($sqldb)) {   <br/>
    echo "No sqldb service instance bound. Please bind a sqldb service instance     before";   <br/>
    return;    <br/>
}    <br/>
     <br/>
$sqldb_config = $services_json["sqldb"][0]["credentials"];       <br/>
       <br/>
// create DB connect string   <br/>
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};";                   <br/>
$conn_string .= "DATABASE=" . $sqldb_config["db"] . ";";     <br/>
$conn_string .= "HOSTNAME=" . $sqldb_config["host"] . ";";   <br/>
$conn_string .= "PORT=" . $sqldb_config["port"] . ";";     <br/>
$conn_string .= "PROTOCOL=TCPIP;";     <br/>
$conn_string .= "UID=" . $sqldb_config["username"] . ";";    <br/>
$conn_string .= "PWD=" . $sqldb_config["password"] . ";";     <br/>
   <br/>
// connect to database      <br/>
$conn = db2_connect($conn_string, '', '');       <br/>

I am not getting any response or I don't know db2_connect statement execute or not.

Please help me.

Upvotes: 0

Views: 295

Answers (3)

v.bontempi
v.bontempi

Reputation: 1562

db2_connect returns false if connection failed, otherwise returns the connection resource to use. http://php.net/manual/en/function.db2-connect.php

In case of error you can use db2_conn_err to get the error got in the last connecting attempt

Upvotes: 0

whitfiea
whitfiea

Reputation: 1943

If you want to know if the connection was successful or not then add the following code after the db2_connect call:

   if ($conn) {
        echo "Connection succeeded.";
    }
    else {
        echo "Connection failed.";
    }

Upvotes: 1

Narendra Kumar
Narendra Kumar

Reputation: 144

I think you need to check your database which bind to your project or not using cf command or use gui also.

Upvotes: 1

Related Questions