0

我正在将 PHP 应用程序从 MySQL 转换为 DB2,IBM_DB2 和 MySQL PHP 扩展之间似乎存在一些我无法理解或解决的不同之处。

db2_fetch_array()db2_connect()如果我在包含and的函数之外调用它,似乎无法从记录集中返回一行db2_exec(),即使返回的记录集存储为对象属性并且在稍后调用时仍返回资源 ID 号。

谁能看到为什么对getNextRow()下面方法的调用无法返回记录集中的下一行?这在 MySQL 中运行良好。

class TestClass
{
//Properties
public $result; //Holds the record set returned from a query
public $sql; //Holds the value of the last sql query sent

    function goQuery($sql)
    {
        $this->sql = $sql;

        if($con = db2_connect("MASTER", "DB2USER", "DB2PASSWORD"))
        {
            echo "<li>Connected to master database</li>";   
        }

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

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

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

$q = new TestClass();

echo "<h2>Running query TestClass->goQuery()</h2>";
echo "<ul>";
$q->goQuery("SELECT * FROM users");
echo "</ul>";

echo "<h2>Trying to fetch a result using TestClass->getNextRow()</h2>";
echo "<ul>";
$row = $q->getNextRow();
echo "</ul>";

这当前产生此输出:

Running query TestClass->goQuery()

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
Trying to fetch a result using TestClass->getNextRow()

No results found in Resource id #12
4

1 回答 1

0

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

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

看起来 DB2 连接和所有返回的资源在调用它们的函数结束时停止存在 - 除非使用持久连接。

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

于 2012-05-25T02:12:40.820 回答