Home arrow PHP Tutorials

Translate This Website
Sunday, 07 September 2008
Web Development
Request a Quote
Script Categories
RSS Media Grabber Funny Videos & Pictures Life Stories (Flash) Video Search & Download Video Downloader Script Country on Sale Advanced Polls Phone Upload/SMS Scripts PhotoCube Script SlideShow Creation Script Make Confessions Scripts Guitar Tabs Scripts Create Custom Smiley Script Short URLs & Subdomains Hot Or Not Game Scripts Put your text on images Watermark & Image Hosting Joomla Components Low Cost Scripts
Partners
WEBSITE TRAFFIC
Pissed Customers
Credit Cards
Broadband Tweaks & Info
eBay Sniping Software
Custom Programming
Scripts and Stuffs
Hot php scripts
Add your scripts
Script Search
php scripts directory
script heaven
the cgi site
Scripts
Software Development
Free Icons
SEO Services


Errors and How to Fix Them Print E-mail

This tutorial will briefly describe some of the common errors in PHP.

I get a myriad of e-mails from visitors who get a particular error whenever they're coding, or doing one of the PHP tutorials. Most of the errors which people get are very similar so I thought I'd go through some of the most common errors, what causes them, and how to fix it. The three most common error types are:

  1. Parse Errors

  2. Header Errors

  3. mySQL Result Source Errors

Parse Errors

What Error Says:

Parse error: parse error, unexpected T_STRING in /home/virtual/site5/fst/var/www/html/akash/errors.php on line 11


Cause of Error

A parse error is a runtime error whenever the PHP compiler is trying to compile your code. Its usually something simple, and can usually be fixed fairly easily. The problem is the syntax of your code, perhaps you forgot a semi-colon on one of the lines. Another infamous parse error is forgetting a double quote (") or an end bracket (}) after you started one. In that case, the error will not be on the line where the brace of quote should be, but might be at the end of your program.


How to Fix Them

Well, the easiest way to go about fixing parse errors is to find the line which PHP is saying caused the error. If you notice that you forgot a simple semi-colon, then insert it and you'll be on your way. If the line it says is the last line of your code, then you need to go back and look through your code to see if all the { } and " are set up properly. One way to check the brackets is to tab them properly and see if they line up. To check for the ", you can try to use a color coded editor, which will usually indicate anything inside a " in a different color.


Header Errors

What Error Says:

Warning: Cannot add header information - headers already sent by (output started at /home/virtual/site5/fst/var/www/html/akash/errors.php:9) in /home/virtual/site5/fst/var/www/html/akash/errors.php on line 10


Cause of Error

HTTP Headers are specific functions defined by PHP which are required to be sent before any output from your script. This means that if it is a header function, then you must place it before any physical output, such as or even a whitespace. Fortunately, there aren't too many header functions out there, but if you get this error, then I guess you've found one. Two of the most important ones are the redirect function in PHP:

Header("location: http://ww.spoono.com");

and the

setcookie()

function.


How to Fix Them

Chances are that if you have a header error, you're trying to pull a function that's supposed to be before any other ouput in your file after you've placed an output. Go to the line where the error is, select that section and punch it up to the top of your code. Also, if you must have an output before the header function, there is an alternative. Place the code at the top before any other text, and then place later on near wherever you must run the header script. That should be about it.


mySQL Result Source Errors

What Error Says:

Warning: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/virtual/site5/fst/var/www/html/tutorials/php/error/index.php on line 3


Cause of Error

Contrary to what the line number might say, this error is sort of tricky because more times than not, the error occurs on the $result variable you use to define the loop. For example say your code looks like the following:

<?
$result = mysql_query("select * from shoutbox ORER by id desc limit 5");
//problem on the above line, ORDER is misspelled
while($r=mysql_fetch_array($result))
{
$time=$r["time"]; //getting each variable from the table
$id=$r["id"];
$message=$r["message"];
$name=$r["name"];
}
?>

The problem displayed would be with the mysql_fetch_array() function on the while loop. However, the real problem is that the $result variable misspells the word "order" on line 1. The $result variable is not executed till the while loop runs on line 3, which is why the error line is given on that line.


How to Fix Them

The first thing to do when you see the mySQL Resource error is to go to the line which PHP says is causing the problem. Next, make sure that the syntax on the line is right, more times than not it will be. Now, the main thing to check where most of the problem usually hides is inside the $result variable definition. For example, on our code up there, the problem is not on the where PHP says it is, but rather on the $result variable defined above it. 99.9% of the time, there is a problem with that, whether it be something misspelled or undefined. To find out the particular problem, you can replace your $result with the following code: , which will display the cause of the error. Go check and fix that and you'll be on your way.


Other Comments

One final comment I did infact want to make is that sometimes you receive errors from your script but everything seems to run fine. In such a case that it just gives you a warning and everything looks good, you can place an "@" before that variable where the error is and it should hide the error. For example, if you write , it will not display the error it finds. Just an important note that might help you.

 

Credit: www.spoono.com 





Reddit!Del.icio.us!Facebook!Slashdot!Netscape!Technorati!StumbleUpon!Newsvine!Furl!Yahoo!Ma.gnolia!Free social bookmarking plugins and extensions for Joomla! websites!
 
< Prev   Next >
Services
Script Installation
Hosting
Free Image Hosting
PageRank Checker
Login Form
On Sale
PhotoCube
PhotoCube
$44.99
$24.99
You Save: $20.00
Add to Cart
Home Announcements Support Newsletter Forum PHP Articles PHP Tutorials Scripts FAQs Free Scripts Links Contacts
Copyrighted © 2008 phppod.com