How to print out the real sql, not just the prepared statement?

We're using Doctrine, a PHP ORM. I am creating a query like this:

$q = Doctrine_Query::create()->select('id')->from('MyTable');

and then in the function I'm adding in various where clauses and things as appropriate, like this

$q->where('normalisedname = ? OR name = ?', array($string, $originalString));

Later on, before execute() -ing that query object, I want to print out the raw SQL in order to examine it, and do this:

$q->getSQLQuery();

However that only prints out the prepared statement, not the full query. I want to see what it is sending to the MySQL, but instead it is printing out a prepared statement, including ? 's. Is there some way to see the 'full' query?


Doctrine is not sending a "real SQL query" to the database server : it is actually using prepared statements, which means :

  • Sending the statement, for it to be prepared (this is what is returned by $query->getSql() )
  • And, then, sending the parameters (returned by $query->getParameters() )
  • and executing the prepared statements
  • This means there is never a "real" SQL query on the PHP side — so, Doctrine cannot display it.


    An example..

    $qb = $this->createQueryBuilder('a');
    $query=$qb->getQuery();
    

    Show SQL: $sql=$query->getSQL();

    Show Parameters: $parameters=$query->getParameters();


    You can check the query executed by your app if you log all the queries in mysql:

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

    there will be more queries not only the one that you are looking for but you can grep for it.

    but usually ->getSql(); works

    Edit:

    to view all the mysql queries I use

    sudo vim /etc/mysql/my.cnf 
    

    and add those 2 lines:

    general_log = on
    general_log_file = /tmp/mysql.log
    

    and restart mysql

    链接地址: http://www.djcxy.com/p/44874.html

    上一篇: PHP:在准备好的语句上获取插入确认

    下一篇: 如何打印出真正的SQL,而不仅仅是准备好的声明?