0

db2_fetch_assoc() isn't finding the next row when called from certain places - even though there are more rows in the recordset.

goQueryDB2() connects to the database and runs a query. It is able to return the first row in the recordset using db2_fetch_assoc() and the second row in the recordset by calling the outside function getNextRowDB2() (which does the same thing).

The recordset resource is returned from that function ($result). However when getNextRowDB2() is then called again with $result supplied as a parameter, the third row in the recordset is NOT returned - in fact it can't find anything and throws a warning (Warning: db2_fetch_assoc() [function.db2-fetch-assoc]: Fetch Failure).

You can see from the output of "No results found in Resource id #12" the name of the resource has been effectively passed to the getNextRowDB2() function - but for some reason it no longer find any rows in that recordset.

Can anyone tell me what is going on? Or even confirm if you agree this code looks ok?

Is there something that makes the DB2 recordset resource inaccessible outside the function in which it was called?

This worked ok in MySQL (I have to convert to DB2).

So this code:

function goQueryDB2($sql)
{
  if($con = db2_connect("MASTER", "USER", "PASSWORD"))
  {
    echo "<li>Connected to master database</li>";   
  }

  if($result = db2_exec($con, $sql))
  {
    echo "<li>SQL Query ran successfully</li>"; 
  }

  //Just proving some data can be returned as this point
  $row = db2_fetch_assoc($result);

  if(sizeof($row) > 0)
  {
    echo "<li>db2_fetch_assoc returned a record in the resultset (Resource ID: " . $result . ") that has " . sizeof($row) . " fields</li>"; 
  }

  //Just proving some data can be returned as this point
  $row = getNextRowDB2($result);
  if(sizeof($row) > 0)
  {
    echo "<li>getNextRow() returned a record in the resultset (Resource ID: " . $result . ") the has " . sizeof($row) . " fields</li>"; 
  }

  return $result;
}

function getNextRowDB2($result)
{
    if($row = db2_fetch_assoc($result))
    {
        return $row;    
    }
    else
    {
        echo "<li>No results found in " . $result . "</li>";    
    }
}

echo "<h2>goQueryDB2()</h2>";
$result = goQueryDB2("SELECT * FROM users");

echo "<h2>getNextRowDB2()</h2>";
$row = getNextRowDB2($result);

Produces this output:

goQueryDB2()
Connected to master database
SQL Query ran successfully
db2_fetch_assoc returned a record in the resultset (Resource ID: Resource id #12) that has 25 fields
getNextRow() returned a record in the resultset (Resource ID: Resource id #12) the has 25 fields
getNextRowDB2()

Warning: db2_fetch_assoc() [function.db2-fetch-assoc]: Fetch Failure in /home/portal/includes/functionsSecurity.php on line 262
No results found in Resource id #12
4

1 回答 1

0

我能够通过使用持久连接来解决:

$con = db2_pconnect("MASTER", "USER", "PASSWORD")

我假设即使资源的名称(“Resource id #12”)是从函数传回的,DB2 连接和所有返回的资源在调用它们的函数结束时都会停止存在 - 除非持久连接用来。

这是 DB2 独有的,因为我发现在 MySQL 中实现这一点不需要持久连接。

于 2012-05-25T02:11:31.383 回答