Avatar of APD Toronto
APD Toronto
Flag for Canada asked on

Convert PDO Bindings to Reqular Qury String

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.
PHPMySQL ServerWindows 7SQL

Avatar of undefined
Last Comment
APD Toronto

8/22/2022 - Mon
Ray Paseur

I don't know of an automated way to do this, but if you want to post the complete query string, I'll be glad to try and help.  FWIW, this is one of the reasons I prefer MySQLi over PDO.

You might also consider catching the PDO exception and looking at it with var_dump().  Not sure what you would find, but it would be one of the things I would try first.
APD Toronto

The way of the query looks the same as above (ie- field name/assignment).

How would I do the var_dump() and do you know anything above that MySQL query log?
Ray Paseur

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
APD Toronto

I have a suspicion... if you have defined a field that can accept NULL as value, how do you send a null value?  I'm trying '"NULL"'
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Ray Paseur

Honestly, I have no idea - I never use NULLs!  Maybe try creating a variable and setting it to NULL.  A quoted string containing the word NULL will be just that - a string of data.  So maybe something like this:

$x = NULL;
$statement->bindValue(':my_nullable_column', $x);

In PHP: http://php.net/manual/en/language.types.null.php
APD Toronto

The issue was that I retrieved record from the database, which we already had null values, but I change it other fields and tried to re-save it to the database. The issue with that was that when you retrieve null from the database, they get returned as empty string, but the database will not accept an empty string, so you need to send it back as  'NULL'
Ray Paseur

If you define the fields with default values of '' (empty string) and NOT NULL,  you should be able to get a more reasonable response from the DB engine.  The empty strings will be just that - strings that are empty, and they can be used that way in queries.  I think this can make the programming easier.

All the best, ~Ray
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
APD Toronto

Thanks Ray