PDO and nested fetching

前端 未结 2 412
走了就别回头了
走了就别回头了 2020-12-20 01:13

Let\'s say I have something like this:

$db=new PDO($dsn);

$statement=$db->query(\'Select * from foo\');

while ($result=$statement->fetch())
{
    //d         


        
2条回答
  •  轮回少年
    2020-12-20 01:49

    You should be able to do any other query you want inside your while loop, I'd say ; something like this :

    $db=new PDO($dsn);
    
    $statement=$db->query('Select * from foo');
    
    while ($result=$statement->fetch())
    {
        $statement2 = $db->query('Select * from bar');
        while ($result2=$statement2->fetch()) {
            // use result2
        }
    }
    

    Did you try that ? It should work...


    Still, if you can (if it's OK with your data, I mean), using a JOIN to do only one query might be better for performances : 1 query instead of several is generally faster.

提交回复
热议问题