How do I get a prepared query as it will execute?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
psyclo
Forum Newbie
Posts: 4
Joined: Tue Aug 09, 2011 9:38 am

How do I get a prepared query as it will execute?

Post by psyclo »

I'm new to the mysqli prepared statements, and I'm a bit concerned that what I'm sending to the database is not exactly what is being executed. How can I get the code from MySQL that is to be or was executed, with the parameters in place? Used to be I would just set $query = "select stuff from my tables" and right before I ran the query I would just echo "The query is $query" and I'd know exactly what was going on.

Thanks in advance for your help.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How do I get a prepared query as it will execute?

Post by AbraCadaver »

So far as I know, you can't. Just trust that it escapes and quotes your values.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
psyclo
Forum Newbie
Posts: 4
Joined: Tue Aug 09, 2011 9:38 am

Re: How do I get a prepared query as it will execute?

Post by psyclo »

There has to be a way to get the query as submitted to the database with the parameters populated. There just has to be.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How do I get a prepared query as it will execute?

Post by AbraCadaver »

psyclo wrote:There has to be a way to get the query as submitted to the database with the parameters populated. There just has to be.
Good luck. I Thought so too, but doing a var_dump($stmt) yields nothing resembling the query:

Code: Select all

$id = 1;
$stmt = $mysqli->prepare("SELECT* FROM something WHERE id = ?")) {
$stmt->bind_param("i", $id);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Re: How do I get a prepared query as it will execute?

Post by Pyrite »

Try using the General Query Log feature in MySQL.

http://dev.mysql.com/doc/refman/5.1/en/query-log.html

Also, many PHP frameworks log this as well, with certain debug levels turned on (e.g. CakePHP with Debug Level 2).
Post Reply