Actions

Difference between revisions of "Do not use die to debug"

From Joomla! Documentation

(Importing text file)
 
m (Fixed markup.)
Line 1: Line 1:
====== Don't use die() to debug ======
+
Joomla! 1.5 includes the ability to optionally store the user session in the database.  In PHP 5, because of the order in which it does things, a connection to the database will be closed before it fires the session handlers.
  
Joomla! 1.5 includes the ability to optionally store the user session in the database.  In PHP 5, because of the order in which it does things, an connection to the database will be closed before it fires the session handlers.
+
As a result of this, the common-place practice of using the <code>die( 'test1' );</code> function will result in a plethora of errors being thrown, similar to the following:
  
As a result of this, the common-place practice of using the ''die('test1');'' function will result in a plethora of errors being thrown, similar to the following:
+
Warning: mysqli_query() [<a href='function.mysqli-query'>function.mysqli-query</a>]:
 
+
Couldn't fetch mysqli in C:\Apache2\htdocs\www_site\libraries\joomla\database\database\mysqli.php on line 147
''Warning: mysqli_query() [<a href='function.mysqli-query'>function.mysqli-query</a>]: Couldn't fetch mysqli in C:\Apache2\htdocs\www_site\libraries\joomla\database\database\mysqli.php on line 147''
+
  
 
or:
 
or:
  
''Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'root'@'localhost' (using password: NO) in /var/www/html/libraries/joomla/database/database/mysql.php on line 105''
+
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]:
 +
Access denied for user 'root'@'localhost' (using password: NO) in /var/www/html/libraries/joomla/database/database/mysql.php on line 105
  
 
In order to stop execution gracefully, you need to use the following code:
 
In order to stop execution gracefully, you need to use the following code:
  
<source>
+
<source lang="php">
 
echo 'Test';
 
echo 'Test';
 
$mainframe->close();
 
$mainframe->close();
Line 20: Line 20:
 
If you are developing your own component, you might like to include your own utility function to provide this functionality:
 
If you are developing your own component, you might like to include your own utility function to provide this functionality:
  
<source>
+
<source lang="php">
 
function stop($msg = '')
 
function stop($msg = '')
 
{
 
{
Line 28: Line 28:
 
}
 
}
 
</source>
 
</source>
 +
<noinclude>[[Category:Development]]</noinclude>

Revision as of 18:07, 28 December 2008

Joomla! 1.5 includes the ability to optionally store the user session in the database. In PHP 5, because of the order in which it does things, a connection to the database will be closed before it fires the session handlers.

As a result of this, the common-place practice of using the die( 'test1' ); function will result in a plethora of errors being thrown, similar to the following:

Warning: mysqli_query() [<a href='function.mysqli-query'>function.mysqli-query</a>]:
Couldn't fetch mysqli in C:\Apache2\htdocs\www_site\libraries\joomla\database\database\mysqli.php on line 147

or:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]:
Access denied for user 'root'@'localhost' (using password: NO) in /var/www/html/libraries/joomla/database/database/mysql.php on line 105

In order to stop execution gracefully, you need to use the following code:

echo 'Test';
$mainframe->close();

If you are developing your own component, you might like to include your own utility function to provide this functionality:

function stop($msg = '')
{
    global $mainframe;
    echo $msg;
    $mainframe->close();
}