Common Errors and What They Mean


PHP has many, many different error messages, ranging from a general error such as "Call to undefined function" to a specific error such as "MySQL Connection Failed." Although no one can cover all the possible error messages, I can cover the most commonly seen error messages.

0 is not a * Result index

This means that your query to an * ( * represents the database type) database failed and the result resource returned by the query function is not valid. To eliminate this error, do some error checking in your script:

 <?php $sth = @mysql_query($stmt, $dbh)   or die("Cannot execute Query"); ?> 

By prepending the @ sign to the function call, we suppress PHP's error messages for that function call. The next interesting section of this code snippet is the " or die... " part. Here we check whether the function returns ; if so, we stop the script execution and print out our own error message.

Call to undefined function: *

This means that you have requested a function that is neither a built-in function nor a user -defined function. This is commonly done when you prepend a $ to the function like so:

 <?php $echo("hello"); ?> 

This would fail because you accidentally put the $ sign before the function, so it looks for the $echo variable which is not in the script. Because it doesn't find the $echo variable, $echo("hello") ends up being equivalent to ("Hello") .

Another common error in PHP 3 was to call a function before it was declared. But now that Zend has support for run-time binding of function names , allowing you to declare a function after it is called in the script, this problem is nonexistent.

Finally, you'll get this message if you try to call a function from an extension that has not been compiled into your version of PHP.

PHP Timed out!

Large, memory- intensive scripts tend to take a while to run. Therefore, after a certain amount of time, PHP will stop executing and return this error message. There are two ways to solve this: One way is to make your script more efficient, or perhaps break your script up into smaller scripts that achieve the same purpose. The second way is to increase the maximum execution time either in your php.ini file or by using the set_time_limit() function:

 <?php set_time_limit(70); ?> 

This example would make the script time out after 70 seconds.

You can also turn off the time limit by passing as the argument to set_time_limit() .

Premature End of script headers

This error can be a major pain. The most that I can tell you about this error is that one of two things occurred: The first and most likely occurrence is that you didn't correctly install PHP. For more information on how to install PHP, refer to Appendix A, "PHP Installation." The second occurrence is if PHP is installed as a CGI rather than a module, the path at the top of your script is probably not correct. Remember that you need the -q after the PHP script location and the #! declaration must be the first line of the script.

Headers already sent

This error occurs when you have already sent the body of your Web page and then you send additional headers via setcookie or the header function. In general, you must send your headers and set cookies before any other output is sent. If, for some reason, you need to send output before you enable certain headers, set the output buffering option in your PHP script to true . However, please note that after you enable output buffering, you will notice a slight performance lag.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net