Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:59:57+00:00 2026-06-15T14:59:57+00:00

Should PDO prepared statements be freed up after use? And if so, how? Specifically

  • 0

Should PDO prepared statements be freed up after use? And if so, how? Specifically I’m asking about MySQL – how can you, and should you, call DEALLOCATE PREPARE though PDO. (Edit: To clarify, this question is not referring to emulated prepares, but real prepares. )

Also – will this free the results set (when large)?

Explanation:

I have seen code along the lines of

$stmnt = $db->prepare($sql);
$stmnt->execute($aParams);
$stmnt = null;

which led me to wondering what this does, when, and if f unset($stmnt); would be different?

The manual indicates that

When the query is prepared, the database will analyze, compile and
optimize its plan for executing the query. […] By using a prepared
statement the application avoids repeating the
analyze/compile/optimize cycle.

which tends to suggest you should unallocate the statement, and MySQL has the capability. So,

  1. Can you call DEALLOCATE PREPARE, and how
  2. Should you do it?
  3. And can anyone confirm that setting statement to null (or unsetting the statement) will do the same as “free_result” for mysql_ and mysqli_?
  4. Does it happen immediately, or does it wait for garbage collector to kick in?

For completeness, another SO question referring to “free_result” and “close” functions for mysqli_() suggests that freeing the statement actually adds time (unless you have large memory usage and need the space). But “free_result” is different from freeing the SQL server from having the prepared statment cached.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:59:58+00:00Added an answer on June 15, 2026 at 2:59 pm

    Should PDO prepared statements be freed up after use? And if so, how?

    In the context of MySQL? No. Why?

    PDO emulates prepared statements by default. This means that PDO itself does the parameter replacement, escaping, etc, and sends chunks of SQL down the line instead of using native prepared statements.

    While you can turn it on, you still don’t need to expressly close the handle unless you are also using unbuffered queries. Merely letting the statement handle go out of scope or setting it to null will not close the cursor. Again, this only matters if you’re using unbuffered queries. If you are not, then letting it go out of scope or setting it to null is enough to close the handle cleanly.

    You also linked to DEALLOCATE PREPARE. That syntax is only needed when manually calling PREPARE with an SQL string. This is a completely and totally separate action than MySQL C-level API-based prepared statements, which is what PDO_MYSQL is using. (Okay, maybe you’re using mysqlnd, but it’s effectively the same thing.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two questions: To benefit from PDO prepared statements, should I first prepare
I'm new to PHP and PDO, and I try to use prepared statements here.
PDO has the option PDO::ATTR_EMULATE_PREPARES which controls if prepared statements should be emulated only
Using PHP v. 5.2.14 and PDO-MySQL extension. I am new to prepared statements. Need
I use PDO prepared statements to insert records. And uses lastInsertId to get id
Possible Duplicate: MySQL / PDO / Prepared Statements - All a big jump, a
I was told today that I should really be using PDO and prepared statements
Should I use php PDO or normal mysql_connect to execute database queries in PHP?
I have a web application using PHP and PDO with SQLSRV prepared statements to
Where and when do you use the quote method in PDO? I'm asking this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.