PDO and nested fetching

前端 未结 2 418
走了就别回头了
走了就别回头了 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:48

    Sounds like what you are trying to accomplish is getting related data for the record you're looking at, why not just JOIN them in at the first query? The database will be better at connecting the dots internally than any amount of code can do externally.

    But to answer your question, I don't see the harm in opening another connection to the same DSN, most likely thing to happen is that you get another instance of the PDO object pointing to the same actual connection. Also, but depending on the amount of data you're expecting you could just fetchAll and loop over a php array.

提交回复
热议问题