如何在 zend 框架中打印精确的 sql 查询?

发布于 2024-12-09 02:36:21 字数 615 浏览 1 评论 0原文

我有以下一段从模型中获取的代码,

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

当我在 zend 中使用更新查询时,

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

我想知道确切的 mysql 查询。有没有可能的方法在 zend 中打印 mysql 查询?善意的建议

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 ,

$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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

最舍不得你 2024-12-16 02:36:21

选择对象在 Zend Framework 中具有 __toString() 方法。

来自 Zend Framework 手册:

$select = $db->select()
             ->from('products');

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

// The output is the string:
//   SELECT * FROM "products"

另一种解决方案是使用 Zend_Db_Profiler。

$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

Select objects have a __toString() method in Zend Framework.

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 the 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

依 靠 2024-12-16 02:36:21

来自>= 2.1.4

echo $select->getSqlString()

from >= 2.1.4

echo $select->getSqlString()
阳光的暖冬 2024-12-16 02:36:21

我已经浏览了数百页,用谷歌搜索了很多,但没有找到任何确切的解决方案。
最后这对我有用。无论您处于控制器或模型中的哪个位置。这段代码在任何地方都对我有用。只要使用这个

//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;

最后这个东西对我有用。

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.

清风不识月 2024-12-16 02:36:21

您可以使用 Zend_Debug::Dump($select->assemble()); 来获取 SQL 查询。

或者您可以启用 Zend DB FirePHP 分析器 它将以简洁的格式为您提供 Firebug 中的所有查询(甚至是 UPDATE 语句)。

编辑
使用 FirePHP 进行分析也适用于 FF6.0+(不仅适用于链接中建议的 FF3.0)

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

Or you can enable Zend DB FirePHP profiler which will get you all queries in a neat format in Firebug (even UPDATE statements).

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

我最亲爱的 2024-12-16 02:36:21

现在在 Zend2 上:

$select->getSqlString();

显示生成的 SQL来自 ZendDbSql 对象

Now on Zend2:

$select->getSqlString();

Displaying the generated SQL from ZendDbSql object

十雾 2024-12-16 02:36:21

你可以打印..

print_r($select->assemble());

you can print..

print_r($select->assemble());
找个人就嫁了吧 2024-12-16 02:36:21
$statement = $this->sql->getSqlStringForSqlObject( HERE GOES Zend\Db\Sql\SelectSQL object );

echo "SQL statement: $statement";

例子:

$select = $this->sql->select();
...
$select->from(array( 'u' => 'users' ));
$select->join(...
$select->group('u.id');
...
$statement = $this->sql->getSqlStringForSqlObject($select);
echo $statement;
$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;
罪#恶を代价 2024-12-16 02:36:21

甚至更短:

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

更短:

echo  $select .""; die;

even shorter:

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

and more shorter:

echo  $select .""; die;
很快妥协 2024-12-16 02:36:21

这个来自 Zend Framework 文档(即更新):(

echo $update->getSqlString();

奖励)我在我自己的模型文件中使用这个:

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

祝你有美好的一天:)

This one's from Zend Framework documentation (ie. UPDATE):

echo $update->getSqlString();

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

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

Have a nice day :)

等待我真够勒 2024-12-16 02:36:21

使用这个:-

echo $select->query();

Zend_Debug::dump($select->query();

Use this:-

echo $select->query();

or

Zend_Debug::dump($select->query();
眼泪都笑了 2024-12-16 02:36:21

查看 Zend_Db_Profiler。这允许您在准备和执行任何 SQL 语句时记录该语句。它适用于 UPDATE 语句以及 SELECT 查询。

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

最后的乘客 2024-12-16 02:36:21

我用这种方式做到了

$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();

I have done this by 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();
红ご颜醉 2024-12-16 02:36:21

从探查器或查询对象返回的查询将包含占位符(如果您使用这些占位符)。

要查看 mysql 运行的确切查询,您可以使用常规查询日志。

这将列出自启用以来已运行的所有查询。
收集样本后,请不要忘记禁用此功能。
在活动服务器上;该日志可能会很快填满。

从 mysql 终端或查询工具(例如 MySQL Workbench)运行:

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

然后运行查询。
结果存储在“mysql.general_log”表中。

SELECT * FROM mysql.general_log

禁用查询日志:

SET GLOBAL general_log = 0;

验证它已关闭:

SHOW VARIABLES LIKE 'general%';

这帮助我找到占位符未被 zend db 替换的查询。用探查器看不到这一点。

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.

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:

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

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

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.

相权↑美人 2024-12-16 02:36:21
$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);
$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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文