php 如何在 zend 框架中打印精确的 sql 查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7723657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print exact sql query in zend framework ?
提问by mymotherland
I have the following piece of code which i taken from 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 query in zend like ,
当我在 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 any possible way to print the mysql query in zend ? kindly advice
在这里我想知道确切的 mysql 查询。有没有办法在 zend 中打印 mysql 查询?友善的建议
回答by Mark Basmayor
Select objects have a __toString() method in Zend Framework.
选择对象在 Zend 框架中有一个 __toString() 方法。
From the Zend Framework manual:
从 Zend 框架手册:
$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 the Zend_Db_Profiler. i.e.
另一种解决方案是使用 Zend_Db_Profiler。IE
$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);
回答by Whisher
from >= 2.1.4
从 >= 2.1.4
echo $select->getSqlString()
回答by Rajan Rawal
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.
最后这件事对我有用。
回答by Christian P
You can use Zend_Debug::Dump($select->assemble());
to get the SQL query.
您可以使用Zend_Debug::Dump($select->assemble());
来获取 SQL 查询。
Or you can enable Zend DB FirePHP profilerwhich will get you all queries in a neat format in Firebug (even UPDATE statements).
或者,您可以启用Zend DB FirePHP 分析器,它将在 Firebug 中以简洁的格式获取所有查询(甚至 UPDATE 语句)。
EDIT: Profiling with FirePHP also works also in FF6.0+ (not only in FF3.0 as suggested in link)
编辑:使用 FirePHP 进行分析也适用于 FF6.0+(不仅在链接中建议的 FF3.0 中)
回答by Walter Caraza
Now on Zend2:
现在在 Zend2 上:
$select->getSqlString();
回答by AvMishra
you can print..
你可以打印..
print_r($select->assemble());
回答by Username
$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;
回答by Vladimir Ch
even shorter:
甚至更短:
echo $select->__toString()."\n";
and moreshorter:
和更短:
echo $select .""; die;
回答by KevDev
This one's from Zend Framework documentation(ie. UPDATE):
这个来自Zend Framework 文档(即更新):
echo $update->getSqlString();
(Bonus) I use this one in my own model files:
(奖励)我在自己的模型文件中使用了这个:
echo $this->tableGateway->getSql()->getSqlstringForSqlObject($select);
Have a nice day :)
祝你今天过得愉快 :)
回答by txyoji
The query returned from the profiler or query object will have placeholders if you're using those.
如果您正在使用这些,从探查器或查询对象返回的查询将具有占位符。
To see the exact query run by mysql you can use the general query log.
要查看 mysql 运行的确切查询,您可以使用一般查询日志。
This will list all the queries which have run since it was enabled. Don't forget to disable this once you've collected your sample. On an active server; this log can fill up very fast.
这将列出自启用以来运行的所有查询。 收集样本后不要忘记禁用此功能。在活动服务器上;这个日志可以很快填满。
From a mysql terminal or query tool like MySQL Workbench run:
从 mysql 终端或查询工具(如 MySQL Workbench)运行:
SET GLOBAL log_output = 'table';
SET GLOBAL general_log = 1;
then run your query. The results are stored in the "mysql.general_log" table.
然后运行您的查询。结果存储在“mysql.general_log”表中。
SELECT * FROM mysql.general_log
To disable the query log:
要禁用查询日志:
SET GLOBAL general_log = 0;
To verify it's turned off:
要验证它是否已关闭:
SHOW VARIABLES LIKE 'general%';
This helped me locate a query where the placeholder wasn't being replaced by zend db. Couldn't see that with the profiler.
这帮助我找到了一个查询,其中占位符没有被 zend db 替换。用分析器看不到。