无法使用PDO在PHP中运行嵌套查询(从第一个查询的每个输出中获取新的结果集)

时间:2016-04-08 17:47:51

标签: php pdo sqlsrv pdo-odbc

我试图从PHP v5.4升级到php v7.0,我遇到了无法运行内部嵌套查询的问题。我正在使用pdo_sqlsrv作为我的SQL服务器,但由于它的PDO驱动程序尚不适用于PHP v7.0,我切换到了pdo_odbc。能够运行特定于sqlsrv的嵌套查询吗?我已经添加了一些测试代码以使我的情况变得清晰。 PDO实例的DBTEST类:

class DBTEST{
    private static $instance = null;
    public static function getInstance(){
        $userid = "testUser";
        $pwd = "love-da-poe-poes";
        self::$instance = new PDO("odbc:testdb", $userid, $pwd);
        return self::$instance;
    }
}

嵌套查询:

$parentQuery = "Select pid from Tree";
$parentResult = DBTEST::getInstance()->prepare($parentQuery);
$parentResult->execute();

$childQuery = "Select cid, cname from Tree where pid = ?";
$childResult = DBTEST::getInstance()->prepare($childQuery);

while($parentObject = $parentResult->fetchObject()){
    $childResult->bindParam(1, parentResult->pid, PARAM_INT);
    $childResult->execute();
    while($childObject = $childResult->fetchObject()){
        //do something with results
    }
}

0 个答案:

没有答案