How do you debug CodeIgniter applications?
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.
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?
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:
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.
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:
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
You can use Xdebug and it's a good idea to start with Test Driven Development, in PHP you can use PHP Unit for that.
If you've not already looked at the amazing web development extension for firefox called Firebug you should check it out!
Firebug has an extension called FirePHP which enables you to debug PHP applications. Someone has also made an plugin fore CodeIgniter called FireIgnition.
It enables you log variables and so forth making it easier to see what is going on as pages are being executed.
I find dbug to be quite helpful when I need to throw a variable, object, array, or resource on the screen to view it:
An article for integrating it into CodeIgniter can be found here.
You did if ($query->num_rows() > 0)
. This will print an error if there are MORE THAN 0 rows returned.
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.
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
}
精彩评论