37

The PHP documentation on closeCursor() says that it

frees up the connection to the server so that other SQL statements may be issued, but leaves the statement in a state that enables it to be executed again.

When I have used the command though it doesn't seem to matter if it is there or not in between my query statements, and I am beginning to wonder if I need it at all.

Is it different to use it for database calls that don't return data vs. those that do?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Rafie
  • 569
  • 1
  • 5
  • 10
  • 11
    You'll know that you have to use `closeCursor()` when PDO complains that it can't execute queries while other unbuffered queries are being executed. That usually happens when you try to invoke a stored procedure via `PDOStatement` object. Bottom line - unless you get the mentioned error, don't worry about it. – N.B. Oct 11 '12 at 16:13
  • Another SO question that may interest you: http://stackoverflow.com/questions/1046614/do-sql-connections-opened-with-pdo-in-php-have-to-be-closed – Benny Hill Dec 20 '12 at 22:40
  • What driver are you using for PDO? That might make a difference. MySqli pdo, sqlsrv pdo? – Nick Rolando May 30 '14 at 18:43

1 Answers1

7

This depends on the driver used. I think for mysql this will do nothing else than clear the result of the statement. After calling closeCursor() you cannot call fetch() anymore. However executing the statement again should not be a problem. Try to look at mysql_free_result(), it does a similar thing.

mosch
  • 1,005
  • 1
  • 12
  • 24