下面是我的.php应用程序连接到dash-DB实例的代码
//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 ..。
我在最近的日志文件中看到以下错误:
“警告: db2_fetch_assoc()要求参数1为资源,布尔值位于/home/vcap/app/www/login.php第44行,”
我甚至在代码中添加了以下内容:
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中的值完全相同。
发布于 2016-07-25 19:47:39
几个小时后,我的头撞在墙上,我找到了答案。
我使用的代码来自于我在IBM SQLDB上使用的连接和查询...
出于某种原因,我不得不对其进行了一些更改,以便与IBM dashDB一起使用。
我不得不改变:
$stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE));至
$stmt = db2_exec($conn, $sql);https://stackoverflow.com/questions/38564918
复制相似问题