How to print exact sql query in zend framework?

I have the following code snippet that I took from the model,

... $select = $this->_db->select() ->from($this->_name) ->where('shipping=?',$type) ->where('customer_id=?',$userid); echo $select; exit; // which gives exact mysql query. ..... 

When I use update request in zend,

 $up_value = array('billing'=> '0'); $this->update($up_value,'customer_id ='.$userid.' and address_id <> '.$data['address_Id']); 

Here I want to know the exact mysql query. Is there a possible way to print mysql query in zend? kind advice

+68
php mysql zend-framework zend-db
Oct 11 2018-11-11T00:
source share
14 answers

In the Zend Framework, select objects have a __toString () method.

From the Zend Framework manual:

 $select = $db->select() ->from('products'); $sql = $select->__toString(); echo "$sql\n"; // The output is the string: // SELECT * FROM "products" 

An alternative solution would be to use Zend_Db_Profiler. i.e.

 $db->getProfiler()->setEnabled(true); // your code $this->update($up_value,'customer_id ='.$userid.' and address_id <> '.$data['address_Id']); Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQuery()); Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQueryParams()); $db->getProfiler()->setEnabled(false); 

http://framework.zend.com/manual/en/zend.db.select.html

+117
Oct 12 '11 at 3:30
source share

from> = 2.1.4

 echo $select->getSqlString() 
+23
May 13 '13 at 12:28
source share

I traveled hundreds of pages, searched a lot, but I did not find any exact solution. Finally it worked for me. No matter where you are in the controller or model. This code worked for me everywhere. 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 work worked for me.

+15
Mar 01 2018-12-12T00:
source share

You can use Zend_Debug::Dump($select->assemble()); to receive the SQL query.

Or you can enable the Zend DB FirePHP profiler , which will provide you with all the queries in a neat format in Firebug (even for UPDATE statements).

EDIT : Profiling with FirePHP also works in FF6.0 + (not only in FF3.0, as indicated in the link)

+6
Oct 11 2018-11-11T00:
source share

Now on Zend2:

 $select->getSqlString(); 

Display generated SQL from ZendDbSql object

+3
Dec 26 '14 at 20:27
source share

you can print ..

 print_r($select->assemble()); 
+2
Feb 22 '13 at 7:27
source share
 $statement = $this->sql->getSqlStringForSqlObject( HERE GOES Zend\Db\Sql\SelectSQL object ); echo "SQL statement: $statement"; 

Example:

 $select = $this->sql->select(); ... $select->from(array( 'u' => 'users' )); $select->join(... $select->group('u.id'); ... $statement = $this->sql->getSqlStringForSqlObject($select); echo $statement; 
+1
Nov 22 '13 at 15:48
source share

Use this: -

 echo $select->query(); 

or

 Zend_Debug::dump($select->query(); 
0
Oct 11 2018-11-11T00: 00Z
source share

Check out Zend_Db_Profiler . This allows you to register any SQL statement as it is prepared and executed. It works for UPDATE statements as well as for SELECT queries.

0
Oct 12 '11 at 20:12
source share

I did it this way

 $sql = new Sql($this->adapter); $select = $sql->select(); $select->from('mock_paper'); $select->columns(array( 'is_section' )); $select->where(array('exam_id = ?' => $exam_id,'level_id = ?' => $level_id))->limit(1); $sqlstring = $sql->buildSqlString($select); echo $sqlstring; die(); 
0
Sep 28 '16 at 10:02
source share

even shorter:

 echo $select->__toString()."\n"; 

and more shorter:

 echo $select .""; die; 
0
Nov 25 '16 at 5:50
source share

A query returned from the profiler or query object will have placeholders if you use them.

To see the exact query made by mysql, you can use the general query log.

This list will list all the requests that have been executed since its inclusion. Remember to disable this once you have collected your sample. On the active server; this journal can fill up very quickly.

From a MySQL terminal or query tool such as MySQL Workbench, run:

 SET GLOBAL log_output = 'table'; SET GLOBAL general_log = 1; 

then run your query. The results are stored in the table "mysql.general_log".

 SELECT * FROM mysql.general_log 

To disable the query log:

 SET GLOBAL general_log = 0; 

To make sure it is turned off:

 SHOW VARIABLES LIKE 'general%'; 

This helped me find a query in which the placeholder was not replaced with Zend DB. I can not see this with the profiler.

0
Jun 21 '19 at 20:30
source share
 $db->getProfiler()->setEnabled(true); // your code $this->update('table', $data, $where); Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQuery()); Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQueryParams()); $db->getProfiler()->setEnabled(false); 
-one
Feb 06
source share

This is from the Zend Framework documentation (i.e. UPDATE):

 echo $update->getSqlString(); 

(Bonus) I use this in my own model files:

 echo $this->tableGateway->getSql()->getSqlstringForSqlObject($select); 

Have a nice day :)

-one
Jan 10 '19 at 4:42
source share



All Articles