Question

Gone are the days when I did something like this:
WHERE something = '".$query->real_escape_string($your_unsafe_value_here)."'

So I've made the move to prepared statements, but I'm a little confused on how to debug some of my queries now. Here's an example PDO prepared statement that uses named placeholders. I've taken this from the online php manual:

/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
                      FROM fruit
                      WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();

If I need to do a quick test on a query, how do I run something like this in phpmyadmin? (...or is there another mysql GUI that can easily handle these types of queries?)

Was it helpful?

Solution 3

For my setup this is what I ended up doing. Hopefully this solution will help someone else, regardless of my setup details...

1) I turned on query logging in my my.ini file (I'm using Windows / WAMP):

[mysqld]

log = "C:\wamp\logs\queries.log"

2) I needed to tell PDO to emulate prepared statements (I'm using Laravel 4.1):

// Before you run the query...
 $pdo = DB::connection()->getPdo();
 $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES , true);    
// run query here...
// and then die, so it's the last query ran
die();

3) Keep the queries.log file open in notepad++ and just reload the file when prompted by notepad++. Copy & paste the last query into phpmyadmin. Now I can debug the query!

I'll probably later have to come up with a more automated approach, but this seems to work for now.

OTHER TIPS

It is possible to run the Query in Toad for MySQL.. When you run the query you get a pop-up to enter the variables

You can't run PDO queries in phpMyAdmin directly and I don't know any other programs that can do that. You can build your own by writing the code from a form to a file and excuting that code in de background. But that isn't very safe because you can run anything there

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top