Connecting to a Db2 database in a PHP application

Define a connection between a PHP application and the Db2 database, BLUDB.

Procedure

  • Scenario 1: Connecting from outside IBM Cloud®:
    1. Download the Db2 driver package from the web console, and then install the driver package on the machine where your PHP application will run.
    2. Verify prerequisites and collect database details and connect credentials.
    3. Use the odbc_connect function to connect to the BLUDB database.
      PHP code example:
      
      <?php
      
      $database = "BLUDB";        # Get these database details from
      $hostname = "<Host-name>";  # the web console
      $user     = "<User-ID >";   #
      $password = "<Password>";   #
      $port     = 50000;          #
      $ssl_port = 50001;          #
      
      # Build the connection string
      #
      $driver  = "DRIVER={IBM DB2 ODBC DRIVER};";
      $dsn     = "DATABASE=$database; " .
                 "HOSTNAME=$hostname;" .
                 "PORT=$port; " .
                 "PROTOCOL=TCPIP; " .
                 "UID=$user;" .
                 "PWD=$password;";
      $ssl_dsn = "DATABASE=$database; " .
                 "HOSTNAME=$hostname;" .
                 "PORT=$ssl_port; " .
                 "PROTOCOL=TCPIP; " .
                 "UID=$user;" .
                 "PWD=$password;" .
                 "SECURITY=SSL;";
      $conn_string = $driver . $dsn;     # Non-SSL
      $conn_string = $driver . $ssl_dsn; # SSL
      
      # Connect
      #
      $conn = odbc_connect( $conn_string, "", "" );
      if( $conn )
      {
          echo "Connection succeeded.";
      
          # Disconnect
          #
          odbc_close( $conn );
      }
      else
      {
          echo "Connection failed.";
      }
      ?>
      
      Saving this PHP code example to a script file called C:\sample.php and then running the script from a command line produces the following output:
      
      C:\> php –f sample.php
      
      Connection succeeded.
      
  • Scenario 2: Connecting from a PHP web app in IBM Cloud
    1. From the IBM Cloud catalog, create a new PHP App.
    2. From the "Getting Started" section of the new PHP App in your IBM Cloud dashboard, download the App starter code to your local work directory.
    3. In your IBM Cloud dashboard, create a new connection from your Db2 service to your new PHP App.
      (Creating this Connection in IBM Cloud makes the VCAP_SERVICES environment variable available to your PHP App. The VCAP_SERVICES environment variable contains database details for your Db2 service. Using VCAP_SERVICES is more convenient than hard-coding the database details in your PHP App.)
    4. In your local working directory, update the file index.php to connect to the BLUDB database using the function db2_connect.
      Example:
      
      <!DOCTYPE html>
      <html>
      <head>
          <title>PHP Starter Application</title>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
          <link rel="stylesheet" href="style.css" />
      </head>
      <body>
      <?php
      if( getenv( "VCAP_SERVICES" ) )
      {
          # Get database details from the VCAP_SERVICES environment variable
          #
          # *This can only work if you have used the Bluemix dashboard to 
          # create a connection from your dashDB service to your PHP App.
          #
          $details  = json_decode( getenv( "VCAP_SERVICES" ), true );
          $dsn      = $details [ "dashDB" ][0][ "credentials" ][ "dsn" ];
          $ssl_dsn  = $details [ "dashDB" ][0][ "credentials" ][ "ssldsn" ];
      
          # Build the connection string
          #
          $driver = "DRIVER={IBM DB2 ODBC DRIVER};";
          $conn_string = $driver . $dsn;     # Non-SSL
          $conn_string = $driver . $ssl_dsn; # SSL
      
          $conn = db2_connect( $conn_string, "", "" );
          if( $conn )
          {
              echo "<p>Connection succeeded.</p>";
              db2_close( $conn );
          }
          else
          {
              echo "<p>Connection failed.</p>";
          }
      }
      else
      {
          echo "<p>No credentials.</p>";
      }
      ?>
      </body>
      </html>
      
    5. From your local working directory, push the updates to IBM Cloud by following the instructions in the "Getting Started" section of the PHP App in your IBM Cloud dashboard. Then restart the App in IBM Cloud and view the App in a browser.