<?xml version="1.0" encoding="utf-8"?>
<tutorial>

<description>This tutorial will briefly describe some of the common errors in PHP.</description>
<keywords>tutorial, PHP, tutorials, errors, error, bug, php, mysql</keywords>
<title>Errors and How to Fix Them</title>

<slug>
This tutorial will briefly describe some of the common errors in PHP.</slug>

<text>
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: 
<ol>
   <li>Parse Errors</li>
   <li>Header Errors</li>
   <li>mySQL Result Source Errors</li>
</ol>
<b>Parse Errors</b><br></br>
<b>What Error Says:</b><br></br>
<i><b>Parse error</b>:  parse error, unexpected T_STRING in <b>/home/virtual/site5/fst/var/www/html/akash/errors.php</b> on line <b>11</b></i>
<br></br><br></br>
<b>Cause of Error</b><br></br>
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.
<br></br><br></br>
<b>How to Fix Them</b><br></br>
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.<br></br><br></br>

<b>Header Errors</b><br></br>
<b>What Error Says:</b><br></br>
<i><b>Warning</b>:  Cannot add header information - headers already sent by (output started at /home/virtual/site5/fst/var/www/html/akash/errors.php:9) in <b>/home/virtual/site5/fst/var/www/html/akash/errors.php</b> on line <b>10</b></i>
<br></br><br></br>
<b>Cause of Error</b><br></br>
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 &lt;html&gt; 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: <green>Header("location: http://ww.spoono.com");</green> and the <green>setcookie()</green> function.
<br></br><br></br>
<b>How to Fix Them</b><br></br>
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 <green>&lt;? ob_start ("ob_gzhandler"); ?&gt;</green> at the top before any other text, and then place <green>&lt;? ob_end_flush(); ?&gt;</green> later on near wherever you must run the header script. That should be about it.<br></br><br></br>
<b>mySQL Result Source Errors</b><br></br>
<b>What Error Says:</b><br></br>
<i><b>Warning</b>:  <b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/home/virtual/site5/fst/var/www/html/tutorials/php/error/index.php</b> on line <b>3</b></i>
<br></br><br></br>
<b>Cause of Error</b><br></br>
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:
<![CDATA[
<pre>
&lt;?
$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"];
}
?&gt;
</pre>
]]>
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.<br></br><br></br>
<b>How to Fix Them</b><br></br>
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 <green>&lt;? $r=mysql_fetch_array($result) ?&gt;</green> 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: <green>&lt;? $result = mysql_query("thequery") or die(mysql_error());) ?&gt;</green>, which will display the cause of the error. Go check and fix that and you'll be on your way. <br></br><br></br>
<b>Other Comments</b><br></br>
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 <green>&lt;? while(@$r=mysql_fetch_array(@$result)) ??&gt;</green>, it will not display the error it finds. Just an important note that might help you.
</text>
</tutorial>

