Thursday, April 1, 2010

Simple Debug tips for PHP&MySQL Program

1. Use PHP command to check if there is any syntax error. If no complains, an html file created by the script should be dumped to the screen. Because the script is check from the command line, it is quick and handy.

php -l report.php

2. Always use <?php codes... ?> instead of <? codes... ?> to make sure the PHP code will run in different environment. Especially for those whom cannot modify the file php.ini to enable the short tags for PHP codes.

3. In the php script, always check the return value of a function such as sql_connect or sql_query with "or die(sql_error())" to make sure the function is executed as expected. Following are examples:

mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_query($sql) or die(mysql_error());

4. Check web server error log such as error_log for Apache server. For example, the Apache error log is /var/log/httpd/error_log for RedHatd, CentOS, and Fedora.