php 您如何调试 CodeIgniter 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4260625/
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 do you debug CodeIgniter applications?
提问by Shivaas
I followed steps in a tutorial to learn how to use Code Igniter to build a PHP/MySQL application and upon completion the application fails with no error. I'm using TextMate rather than a full featured IDE.
我按照教程中的步骤学习如何使用 Code Igniter 构建 PHP/MySQL 应用程序,完成后应用程序失败且没有错误。我使用的是 TextMate 而不是功能齐全的 IDE。
How do most developers using Code Igniter debug their applications? Is there something in the framework for bubbling errors and a stack trace into the browser session?
大多数使用 Code Igniter 的开发人员如何调试他们的应用程序?框架中是否存在用于冒泡错误和浏览器会话中的堆栈跟踪的内容?
The specific problem I'd like to address now seems database related. I configured my database, setup autoload.php, and created a controller and model for handling table data, and a view for presenting it. From my accounts_model.php:
我现在要解决的具体问题似乎与数据库有关。我配置了我的数据库,设置了 autoload.php,并创建了一个用于处理表数据的控制器和模型,以及一个用于呈现它的视图。从我的accounts_model.php:
public function getData()
{
//Query the data table for every record and row
$query = $this->db->get('accounts');
if ($query->num_rows() > 0)
{
show_error('Database is empty!');
}
else
{
return $query->result();
}
}
When I run the application, I see "An Error was Encountered\nDatabase is empty!". Since I know the database is not empty and configuration is correct, I need something from the framework to give me a hint whether it is connecting to the database or why the query is empty.
当我运行应用程序时,我看到“遇到错误\n数据库为空!”。由于我知道数据库不为空且配置正确,因此我需要框架中的某些内容来提示我是否连接到数据库或查询为何为空。
采纳答案by Wouter Dorgelo
You can use Xdebugand it's a good idea to start with Test Driven Development, in PHP you can use PHP Unitfor that.
您可以使用Xdebug,最好从Test Driven Development开始,在 PHP 中,您可以使用PHP Unit。
回答by Shivaas
A good practice is to enclose critical code into try/catch blocks so that you can handle exceptions, and then log the stack trace of that error. In my recent experience, I've used try/catch blocks along with show_error() function provided by CodeIgniter to debug and catch errors in my code. Here's an example:
一个好的做法是将关键代码包含在 try/catch 块中,以便您可以处理异常,然后记录该错误的堆栈跟踪。在我最近的经验中,我使用 try/catch 块以及 CodeIgniter 提供的 show_error() 函数来调试和捕获代码中的错误。下面是一个例子:
public function getData()
{
//Query the data table for every record and row
try{
$query = $this->db->get('accounts');
if ($query->num_rows() > 0)
{
show_error('Database is empty!');
}
else
{
return $query->result();
}
} catch(Exception $e){
log_message('debug',$e->getMessage()); // use codeigniters built in logging library
show_error($e->getMessage()); // or echo $e->getMessage()
}
}
PHP Exception class provides various methods to help debug your error. http://www.php.net/manual/en/class.exception.php
PHP Exception 类提供了各种方法来帮助调试您的错误。http://www.php.net/manual/en/class.exception.php
回答by Keyslinger
回答by Malachi
If you've not already looked at the amazing web development extension for firefox called Firebugyou should check it out!
如果您还没有看过名为Firebug 的Firefox 惊人的 Web 开发扩展,那么您应该查看一下!
Firebug has an extension called FirePHPwhich enables you to debug PHP applications. Someone has also made an plugin fore CodeIgniter called FireIgnition.
Firebug 有一个名为FirePHP的扩展,它使您能够调试 PHP 应用程序。有人还为 CodeIgniter 制作了一个名为FireIgnition的插件。
It enables you log variables and so forth making it easier to see what is going on as pages are being executed.
它使您能够记录变量等,从而更容易查看页面正在执行时发生的情况。
回答by Rocket Hazmat
You did if ($query->num_rows() > 0)
. This will print an error if there are MORE THAN 0 rows returned.
你做到了if ($query->num_rows() > 0)
。如果返回了多于 0 行,这将打印错误。
回答by Jan Roelof
I use VS.PHP, an addin to Visual Studio for my PHP work. Very nice. Syntax highlighting, autocompletion, debugging of PHP and JavaScript in the same session, the works.
我使用 VS.PHP,这是我的 PHP 工作的 Visual Studio 插件。非常好。语法高亮、自动完成、在同一会话中调试 PHP 和 JavaScript,有效。
回答by Red
Just curious :
只是好奇 :
$query->num_rows() > 0
if it success then it means that there are some results ...
如果它成功,那么就意味着有一些结果......
I will do something like this ...
我会做这样的事情......
function get_data()
{
$results = $this->db->get("tble");
if($results->num_rows()>0)
{
return $results->result_array();
}
else
{
return array();
}
}
and in controller
并在控制器中
if(empty( $this->model->get_data() ))
{
//do something with data
}
else
{
//no data
}