1

I am trying to log the sql queries when a script is running. I am using zend framework and I already checked zend db profiler and this is not useful as this shows "?" for the values in a insert query..I need the actual values itself so that I can log it in a file. I use getAdapter()->update method for the update queries so I don' know if there is a way to get queries and log it. Please let me know if there is a way to log the queries.

regards

3 Answers 3

5

From http://framework.zend.com/manual/en/zend.db.profiler.html

The return value of getLastQueryProfile() and the individual elements of getQueryProfiles() are Zend_Db_Profiler_Query objects, which provide the ability to inspect the individual queries themselves:

  • getQuery() returns the SQL text of the query. The SQL text of a prepared statement with parameters is the text at the time the query was prepared, so it contains parameter placeholders, not the values used when the statement is executed.

  • getQueryParams() returns an array of parameter values used when executing a prepared query. This includes both bound parameters and arguments to the statement's execute() method. The keys of the array are the positional (1-based) or named (string) parameter indices.

When you use Zend_Db_Profiler_Firebug it will also show you the queries on the returned pages in the Firebug console along with any bound parameters.

Sign up to request clarification or add additional context in comments.

8 Comments

thanks @Gordon, but I am going to run it from command line, so I cannot see it in firebug..any other way?
@Jay yes, use getQueryParams() like it says in the quotation.
I used this but it just shows Array when i run the script..but when i use getQuery(), i see the sql statements
sorry I should have been clear, I used print_r but I see the array with values like Array( [1]=>test [2]=>name....)..its not showing the values as in a query so that I can copy the queries..with getQuery() it shows like INSERT INTO....
@Jay yes, you have to do that by hand. The bound parameters are not inserted into the query before it gets executed. This only happens inside your database system.
|
3

I know you have got your answer though just for reference... I have traversed hundred of pages, googled a lot but i have not found any exact solution. Finally this worked for me. Irrespective where you are in either controller or model. This code worked for me every where. Just use this

//Before executing your query
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$db->getProfiler()->setEnabled(true);
$profiler = $db->getProfiler();

// Execute your any of database query here like select, update, insert
//The code below must be after query execution
$query  = $profiler->getLastQueryProfile();
$params = $query->getQueryParams();
$querystr  = $query->getQuery();

foreach ($params as $par) {
    $querystr = preg_replace('/\\?/', "'" . $par . "'", $querystr, 1);
}
echo $querystr;

Finally this thing worked for me.

Comments

1

There are a few logs MySQL keeps itself.

Most notably:

The binary log (all queries)
Slow query log (queries that take longer than x time to execute)

See: http://dev.mysql.com/doc/refman/5.0/en/server-logs.html

2 Comments

+1 I prefer this way as well, though I don't think it would show the actual values, if prepared statements were used.
@Adrian, the binary log holds all values of all update and insert statements. It handles prepared statements correctly and stores them in the binlog as they are eventually executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.