Is a call to PDOStatement::closeCursor() necessary if the statement object is unset anway?

后端 未结 2 2277
花落未央
花落未央 2021-02-20 13:51

I\'m utilizing a Mysql based PDO connection with PDOStatement (::prepare(); ::execute()) and I\'ve run over some code from a

相关标签:
2条回答
  • 2021-02-20 14:37

    pdo_mysql_stmt_dtor() runs the same cleanup operations as pdo_mysql_stmt_cursor_closer(), so as long as the statement object is either explicitly unset or goes out of scope, the operations will always be performed.

    It is therefore not strictly necessary to call closeCursor() if the statement is about to be destroyed anyway. Personally I would do it anyway as I like to be explicit for readability, but that comes down to personal stylistic preferences.

    Based on the references above, this can only be said for certain about PDO_mysql - for other drivers this may not hold true.

    0 讨论(0)
  • 2021-02-20 14:49

    My understanding of PDOStatement::closeCursor() is that it clears the result of a executed query. But not affects a PDOStatement at all (just the result of its execution).

    From the PHP Documnetation about this method:

    frees up the connection to the server so that other SQL statements may be issued

    and

    This method is useful for database drivers that do not support executing a PDOStatement object when a previously executed PDOStatement object still has unfetched rows.

    let me assume that you should make after every execution and fetch of all data you need form the current query execution a call to the PDOStatement::closeCursor() method. I think the MySQL driver for PDO does this automatically but there seems to be problems with some drivers which does not automatically free up the result.

    So if you switch to another DB driver which does not free up the pending result of the query you run into an error.

    0 讨论(0)
提交回复
热议问题