使用Bluemix PHP App连接到Bluemix上的DashDB

使用Bluemix PHP App连接到Bluemix上的DashDB

问题描述:

I'm failing to connect to the dashdb on bluemix using php. I tried troubleshooting and I think that my error is within the function $conn = db2_connect( $conn_string, "", "" );

What values do I need to put in the empty inverted commas? Please help.

<!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
    $usern = $_POST['un'];
    $passw = $_POST['pw'];
    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
            echo $conn_string;
        $conn = db2_connect( $conn_string, "", "" );
        //echo "<meta http-equiv='refresh' content='0; url=index.php'>";
        if( $conn )
        {
            echo "<p>Connection succeeded.</p>";
            db2_close( $conn );
        }
        else
        {
            echo "<p>Connection failed.</p>";
        }
    }
    else
    {
        //echo "<meta http-equiv='refresh' content='0; url=index.php'>";
        //echo "<p> <a href='index.php'>Missing DB Connection.</p>";
    }
?>
</body>
</html>

I got a solution from bluemix support. The dashdb driver was not properly installed. I got the driver here: https://github.com/ibmdb/db2heroku-buildpack-php

It works perfectly :)

Try replacing double quotes with single quotes

$conn = db2_connect($conn_string, ' ', ' ' );