Aaron Saray

open source programmer,
web developer

entrepreneur, author
and musician

My Blog

contains PHP, Web and business/entrepreneurial related content. Please join in the conversation!

PDO – can you handle identical prepared statements?

I’ve been wondering if I should be concerned about re-preparing a prepared statement when using PDO. Right now, I use code like this when preparing a statement:

1
2
3
4
5
6
7
8
9
10
    public function prep($statement)
    {
        if ($statement != $this->_lastPrepared) {
            /**
             * Store our clear text statement, and then our object
             */

            $this->_lastPrepared = $statement;
            $this->_ps = $this->db->prepare($statement);
        }
    }

I end up storing the last statement and do a quick compare. My concern comes from MySQL’s admission of this: “If a prepared statement with the given name already exists, it is deallocated implicitly before the new statement is prepared.”

So, not knowing the internal workings of PDO, I wonder how they handle it. Do they…

  • Create each prepared statement with the same name, causing them to be deallocated each time
  • Create each one with a random name, so that there are never any deallocations unless you unset the statement?

Anyone have any insight?

This entry was posted in mysql, PHP and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>