我正在php脚本中在db2上运行查询,该查询已成功运行,但是我无法获得它来回显我选择的记录的实际ID。它确实回显了我的成功声明,表明它已成功运行,但是我需要实际的sessionid
才能在另一个查询中进行比较。
在这里,我选择记录,执行查询并检查执行情况,但我也尝试使用fetch_row和result返回单个选定的ID:
$latest_result = "SELECT MAX(SESSIONID) as SESSIONID FROM session";
$prepareSessionMax = odbc_prepare($DB2Conn, $latest_result);
$executeSessionMax = odbc_execute($prepareSessionMax);
while(odbc_fetch_row($executeSessionMax)){
$maxResult = odbc_result($executeSessionMax, "SESSIONID");
echo $maxResult;
}
如何从db2中正确地将sessionID返回到变量中?
答案 0 :(得分:1)
您将错误的参数传递给odbc_fetch_row()
,因为$executeSessionMax
是True还是False,取决于成功执行。
$latest_result = "SELECT MAX(SESSIONID) as SESSIONID FROM session";
$prepareSessionMax = odbc_prepare($DB2Conn, $latest_result);
$executeSessionMax = odbc_execute($prepareSessionMax);
while(odbc_fetch_row($prepareSessionMax )){
// correction here ^^^^^^^^^^^^^^^^^^
$maxResult = odbc_result($executeSessionMax, "SESSIONID");
echo $maxResult;
}
您可以重新编码,因为MAX()只会返回一行,因此也不需要while循环。
$latest_result = "SELECT MAX(SESSIONID) as SESSIONID FROM session";
$prepareSessionMax = odbc_prepare($DB2Conn, $latest_result);
if (odbc_execute($prepareSessionMax)) {
odbc_fetch_row($prepareSessionMax );
$maxResult = odbc_result($executeSessionMax, "SESSIONID");
echo $maxResult;
// if echo gets lost try writing to a file
error_log("maxResult = $maxResult", 3, "my-errors.log");
} else {
// something went wrong in the execute
}
您也可以尝试
$latest_result = "SELECT MAX(SESSIONID) as SESSIONID FROM session";
$result = odbc_exec($DB2Conn, $latest_result);
$rows = odbc_fetch_object($result);
echo $row->SESSIONID;
$maxResult = $row->SESSIONID;