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

时间:2017-03-30 10:32:38

标签: php ibm-cloud dashdb

我未能使用php连接到bluemix上的dashdb。我尝试了故障排除,我认为我的错误是在函数$ conn = db2_connect($ conn_string,"","");

我需要在空的引号中加入什么值?请帮忙。

<!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>

2 个答案:

答案 0 :(得分:0)

尝试用单引号替换双引号

  

$ conn = db2_connect($ conn_string,&#39;&#39;,&#39;&#39;);

答案 1 :(得分:0)

我从bluemix支持获得了解决方案。未正确安装dashdb驱动程序。我在这里找到了司机:https://github.com/ibmdb/db2heroku-buildpack-php

完美无缺:)