2016-07-25 112 views
0

這裏是我的我的.PHP的應用程序的代碼連接到dashDB例如麻煩連接到dashDB上BlueMix

//parse VCAP_SERVICES Environment variable 
    $vcap_services = $_ENV["VCAP_SERVICES"]; 
    $services_json = json_decode($vcap_services,true); 
    $sqldb = $services_json["dashDB"]; 
    if (empty($sqldb)) { 
     echo "No sqldb service instance is bound. Please bind a sqldb service instance"; 
    return; 
} 

//Get Credentials object (db,host,port,username,password) 
$sqldb_config = $services_json["dashDB"][0]["credentials"]; 

$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=". 
    $sqldb_config["db"]. 
    ";HOSTNAME=". 
    $sqldb_config["host"]. 
    ";PORT=". 
    $sqldb_config["port"]. 
    ";PROTOCOL=TCPIP;UID=". 
    $sqldb_config["username"]. 
    ";PWD=". 
    $sqldb_config["password"]. 
    ";"; 


$conn = db2_connect($conn_string, '', ''); //db connection 

$sql = "SELECT * FROM BX1_USERS WHERE Username ='$username'"; 
$foundElements = 0; 

if ($conn) { 
    $stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE)); 
//lLINE 44 that the output.txt fiel referres to starts here 
     while ($row = db2_fetch_assoc($stmt)) { 
      $dbusername = $row['Username']; 
     $dbpassword = $row['Password']; 
     $dbfirstname = $row['FirstName']; 
     $dblastname = $row['LastName']; 
     $foundElements = 1; 
    } 
} 

print $foundElements; 
echo "<br>"; 
print $sql; 
echo "<br>"; 

    if ($foundElements == 1){ 
/// rest of my code here 

我測試數據庫控制檯打印$ SQL和它的作品就好了..但仍$ foundElements = 0 ...

我得到最近的日誌文件,該錯誤:

「 PHP的警告:db2_fetch_assoc()預計參數1是資源,布爾在/ home/VCAP給出/第44行的app/www/login.php, 「

我甚至已將此添加到代碼:

print $sqldb_config["db"]; 
echo "<br>"; 
print $sqldb_config["host"]; 
echo "<br>"; 
print $sqldb_config["port"]; 
echo "<br>"; 
print $sqldb_config["username"]; 
echo "<br>"; 
print $sqldb_config["password"]; 

和頁面顯示完全相同的值作爲我的VCAP。

+0

我將此添加到我的代碼: 如果(空($語句)){ 回聲 「的聲明爲空」; } 和頁面會回顯該消息...所以$ stmt似乎是空的。有任何想法嗎? –

回答

0

幾個小時後,我的頭靠在牆上我找到了答案。

我所用的代碼是從一個連接,我在IBM SQLDB使用的查詢......

回回不管是什麼原因,我不得不改變它一點點與IBM dashDB工作。

我不得不改變:

$stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE)); 

$stmt = db2_exec($conn, $sql);