php – 如何在Phalcon中回显最后一个查询字符串?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 如何在Phalcon中回显最后一个查询字符串?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2394字,纯文字阅读大概需要4分钟。
内容图文
![php – 如何在Phalcon中回显最后一个查询字符串?](/upload/InfoBanner/zyjiaocheng/704/2a8d39b111714c45b55ecd02a76a33c1.jpg)
我在codeigniter上做了很多工作.在codeigniter中,如果需要获取最后执行的查询字符串,我们可以使用:
echo $this->db->last_query();
exit;
但目前我正在研究phalcon,我刚刚在这个框架中处于初级阶段.我很好奇是否有办法在phalcon中回显最后一个查询字符串.
谢谢.
解决方法:
使用原始查询
我们有以下查询:
$phql = 'UPDATE `news` SET `category_id` = 5 WHERE `id` = :id';
$this->db->execute($phql, ['id' => 1]);
我们可以使用以下方法获取调试查询信息:
print_r($this->db->getSQLStatement());
UPDATE
news
SETcategory_id
= 5 WHEREid
= :id
print_r($this->db->getSqlVariables());
Array (
[id] => 1 )
有关DB方法的更多信息,您可以在这里找到:https://docs.phalconphp.com/en/latest/api/Phalcon_Db_Adapter_Pdo.html
使用模型
设置数据库连接和分析器服务:
use Phalcon\Db\Profiler as ProfilerDb;
use Phalcon\Events\Manager as EventsManager;
use Phalcon\Db\Adapter\Pdo\Mysql as MysqlPdo;
$di->set('profiler', function () {
return new ProfilerDb();
}, true);
$di->set('db', function () use ($di) {
$eventsManager = new EventsManager();
// Get a shared instance of the DbProfiler
$profiler = $di->getProfiler();
// Listen all the database events
$eventsManager->attach('db', function ($event, $connection) use ($profiler) {
if ($event->getType() == 'beforeQuery') {
$profiler->startProfile($connection->getSQLStatement());
}
if ($event->getType() == 'afterQuery') {
$profiler->stopProfile();
}
});
$connection = new MysqlPdo(
array(
"host" => "localhost",
"username" => "root",
"password" => "secret",
"dbname" => "invo"
)
);
// Assign the eventsManager to the db adapter instance
$connection->setEventsManager($eventsManager);
return $connection;
});
用它来调试你的查询:
// Send some SQL statements to the database
Robots::find();
Robots::find(
array(
"order" => "name"
)
);
Robots::find(
array(
"limit" => 30
)
);
// Get the generated profiles from the profiler
$profiles = $di->get('profiler')->getProfiles();
foreach ($profiles as $profile) {
echo "SQL Statement: ", $profile->getSQLStatement(), "\n";
echo "Start Time: ", $profile->getInitialTime(), "\n";
echo "Final Time: ", $profile->getFinalTime(), "\n";
echo "Total Elapsed Time: ", $profile->getTotalElapsedSeconds(), "\n";
}
有关Profiler服务的更多信息:https://docs.phalconphp.com/en/latest/reference/models.html#profiling-sql-statements
Phalcon Prophiler小工具
我正在为FabianFülling制作的Phalcon使用一个可爱的调试小部件.您可以在此处查看存储库:https://github.com/fabfuel/prophiler以下操作窗口小部件的示例屏幕截图:
内容总结
以上是互联网集市为您收集整理的php – 如何在Phalcon中回显最后一个查询字符串?全部内容,希望文章能够帮你解决php – 如何在Phalcon中回显最后一个查询字符串?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。