Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,783 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,442 people online right now. Registration is fast and FREE... Join Now!




The giant PHP List of Common Problems

 
Reply to this topicStart new topic

The giant PHP List of Common Problems

JBrace1990
23 Aug, 2008 - 12:36 PM
Post #1

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 476



Thanked: 22 times
Dream Kudos: 350
My Contributions
I've decided to make this giant list over most of the problems that usually occur, and people have the most questions about.

Headers

The most problems that come from headers is the following error:

QUOTE
Warning: Cannot modify header information - headers already sent by (output started at ****) in *** on line ****


This error occurs when some type of data (usually an echo'd statement) is sent before you try to redirect someone. To fix it, you just need to move the redirection header up to the top of your page (around where session_start() should be if you're using it).

Other errors for headers occur when you are trying to force download things, or open images. you need to make sure you're using the correct type of header (IE: location, image/jpeg).

Sessions


certain errors include sessions not holding information (the fix is usually that session_start() does not exist at the top of the page), or errors popping up (meaning session_start is not at the top of the page. it needs to be set before anything is echo'd to the page).

MySQl


MySQL errors are usually very common. Following are the most common errors:

QUOTE
Mixing of GROUP columns
(MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause


this error occurs when you're using certain functions (such as COUNT), and not having a group by at the end. Basically it is telling you that MySQL doesn't know WHAT to count.

QUOTE
MySQL client ran out of memory


This message usually indicates too many connections, and can be fixed by either increasing the amount of concurrent connections your DB can handle, but can usually be fixed by using mysql_close.

QUOTE
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in (****) on line ****


The above error is usually indicative of you either messing up the query that mysql_fetch_array points to, or not pointing to a query, or even that the variable you used (possibly $sql or $result) does not hold a query.

If MySQL returns 1 row less then it should (and you're positive your query is perfect), it's possible that you called something such as mysql_fetch_array or mysql_fetch_assoc once (or more) before you called it in the while loop. Remember that each call moves the pointer ahead by 1 result.

echo errors


echo errors usually occur when you do not escape certain characters, such as in the following quote:
QUOTE
echo "<table border="0">";


it will probably end either a white page, or a message such as :
QUOTE
unexpected T_WHITESPACE ON LINE *.


or:
QUOTE

Parse error: syntax error, unexpected T_LNUMBER, expecting ',' or ';' in **** on line ****


If/Elseif/Else Statements


the most common error for these is flawed logic. in the following statement, the if will never be activated, always leading to the else:

php
<?php
if($i > 1){
echo "Activated";
}
else
{
echo "Not Activated";
}
?>


The error is that $i is never set to anything. Another error is caused by being too strict:

php
<?php
$i = 3;
if($i > 2 && $i < 3)
{
echo "Activated";
}
?>


Since you are too strict (IE: the variable $i needs to be between 2.000~1 to 2.99999~, it will never be activated.

Elseif are the EXACT same as if statements. however in an if/elseif/else statement, only 1 of them are activated. this means that if the IF statement is True, even if the elseif statement is true, it will NOT be activated. If you want to activate 2 of them, use 2 if statements.

Common Errors


Other common errors include missing Semicolons (;­) at the end of a statement as below:
php
echo "Hello World"
echo "There's a php error in here."


Depending on the server information, you'll either end up with a white page or an error message.

Suggestions
  1. Buy or Install a free trial of an IDE (Integrated Development Environment). It highlights any PHP syntax errors, and tells you what the error is. I would reccomend NuSphere PhpED
  2. Read the DIC Tutorials. They're very helpful and usually very informative.
  3. If you're going to ask DIC for help, Please don't be impatient, and make sure to tell us what is wrong. We're not going to go through your entire script just to help you unless you'retrying to help yourself as well.
  4. although many of us here are good at PHP or other coding languages, this does not mean we know what you're talking about. I, for one, do not know things such as Smarty or XamPP, or Apache or any of them.
  5. Have Fun. Help other people. If someone has helped you, chances are the time will come when you can help them back. All of us usually overlook some simple errors (I for one have been known to mess up some simple things, such as += and .= in PHP (which probably results from me learning like 5 languages at a time))

Feel free to post any revisions or elements this could use. I'm hoping to be able to make a thread where someone can look up a common problem and fi it by themselves.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: The Giant PHP List Of Common Problems
23 Aug, 2008 - 12:40 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Nice post JBrace1990, think I'll pin this one so others can easily find it
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: The Giant PHP List Of Common Problems
23 Aug, 2008 - 12:45 PM
Post #3

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 476



Thanked: 22 times
Dream Kudos: 350
My Contributions
Thank you PsyoCoder =D I was hoping someone would... Anything else I should add to it? I ran out of ideas lol
User is offlineProfile CardPM
+Quote Post

Moonbat
RE: The Giant PHP List Of Common Problems
23 Aug, 2008 - 01:55 PM
Post #4

D.I.C Regular
Group Icon

Joined: 30 Jun, 2008
Posts: 391



Thanked: 22 times
Dream Kudos: 600
My Contributions
Here's one that bugged me a little bit one time.
The Code
CODE
while ($result = mysql_fetch_array($query)) {
            echo "$result['data']";
        }

The Error
QUOTE
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

The Solution
You cannot use single quotes inside the double quotes like this. An easy solution, instead of escaping the quotes, is to get rid of the single quotes:
CODE
while ($result = mysql_fetch_array($query)) {
            echo "$result[data]";
        }


This post has been edited by Moonbat: 23 Aug, 2008 - 01:56 PM
User is offlineProfile CardPM
+Quote Post

kummu4help
RE: The Giant PHP List Of Common Problems
25 Aug, 2008 - 09:51 PM
Post #5

D.I.C Head
Group Icon

Joined: 4 Aug, 2008
Posts: 174


Dream Kudos: 25
My Contributions
nice post Jbrace1990

i would like to add following
while comparing for returntypes of function values with true or false it's better to do with === rather sticking to ==.

because sometimes == maynot treat 0(say the return value) as false
i.e
php

if(false==somefunction(returnsfalse))
{
do something;
}


will not compare if the returnvalue is *0*
so always better to do the following

php

if(false===somefunction(returnsfalse))
{
do something;
}

User is offlineProfile CardPM
+Quote Post

akozlik
RE: The Giant PHP List Of Common Problems
26 Aug, 2008 - 06:40 AM
Post #6

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 615



Thanked: 24 times
Dream Kudos: 750
My Contributions
Logic Errors

A lot of people have trouble with their logic when determining whether or not to execute a section of code. I'm talking about the use of ANDs and ORs, specifically with the use of NOT.

The biggest problem I've seen is when somebody is saying something like this in their mind.

"If x is not equal to three or y is not equal to five, do this"

This would result in something like the following code:

php

if ($x != 3 || $y != 5)
{
// code to execute
}


However, this would be incorrect. In your head you are saying OR, but what you really mean is AND. The above code would always return true. What you would be looking for is this:

php

if ($x != 3 && $y != 5)
{
// code to execute
}


Hopefully that will clear up some logic errors. To see the post that this came from, check out:

Simple OR Statement
User is online!Profile CardPM
+Quote Post

Hary
RE: The Giant PHP List Of Common Problems
26 Sep, 2008 - 12:38 AM
Post #7

D.I.C Head
**

Joined: 23 Sep, 2008
Posts: 205



Thanked: 15 times
My Contributions
Unexpected behaviour of an if-statement:
Incorrect usage of an assignment when a comparison is is meant
CODE

if ($a = $b)


This piece of code will assign the value of $b to $a and php will return the value of $b. It will only evaluate to false iff $b == false

Correct usage:
CODE
if ($a == $b)

User is offlineProfile CardPM
+Quote Post

spearfish
RE: The Giant PHP List Of Common Problems
30 Sep, 2008 - 06:28 PM
Post #8

Monkey in Training
Group Icon

Joined: 10 Mar, 2008
Posts: 746



Thanked: 2 times
Dream Kudos: 225
My Contributions
2 things:
First, to add on to this:
php

while ($result = mysql_fetch_array($query)) {
echo "$result[data]";
}

I would suggest making that:
php

while ($result = mysql_fetch_array($query)) {
echo "{$result['data']}";
}

The curly braces allow you to interpolate the data while retaining the single quotes. Without them, the parser FIRST looks for a CONSTANT named data, which can mess with your results.

Second,
One of the biggest things that tripped me up was that when I was first starting, I didn't know all of the built-in functions. You'll have a tough time finding the error in this code:
php

function link($target, $txt) {
$target = htmlentities($target);
$txt = htmlentities($txt);
return "<a href='$target'>$txt</a>";
}

if you don't already know that php has a built in function called link(). So, if php isn't letting you "redeclare" a function you know you haven't decalred yet, check out php.net/{function name} and see if something comes up.

This post has been edited by spearfish: 30 Sep, 2008 - 06:29 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 12:59PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month