We help IT Professionals succeed at work.
Get Started

Convert PDO Bindings to Reqular Qury String

Last Modified: 2015-08-01
Hi Experts,

Is there any way I can convert a PDO Bound query into a regular string?  For example, I have:

$query  = "UPDATE reservations SET "
                .   "user_initials = :user_initials "
                . ", user_id = :user_id "
                . "WHERE id = :id;";
        $db = Database::getDB();
        echo $query . '<br>';
        $statement = $db->prepare($query);
            $statement->bindValue(':user_initials', $book['user_initials']);
            $statement->bindValue(':user_id', $book['user_id']);
            $statement->bindValue(':id', $book['id']);
        $test = "";
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, $test);
        echo '<br>' . $test . '<br>';

Open in new window

However, I actually have 50+ bound values, and I'm getting an error Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' trans_paym_loc = 0 , trans_payee_type = D , trans_payee_id = 1 , trans_paym_dat' at line 1'  in ...

Whenever I get such an error I usually echo my query, copy it into phpMyAdmin, then I can see clearly what values are causing that problem, and go from there.

My question is, how can I translate this into a normal query, so that I can run it in phpMyAdmin?  I have read about PDO::ATTR_EMULATE_PREPARES, which I am trying at the bottom of my script, but I am not getting anything, so I am not sure how to use it. I have also read about MySQL's query log, but I am not sure how to access it.

Any help will be greatly appreciated.
Watch Question
Most Valuable Expert 2011
Author of the Year 2014
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE