Jump to content


Error Messages Explained


11 replies to this topic

#1 Gustavo el Geranie

    Advanced Member

  • Members
  • PipPipPip
  • 716 posts

Posted 07 July 2003 - 07:13 AM

Hi board,

I want to start a new topic about error messages. There are sometimes error messages in PHP, where the text is misleading or not easily understandable.
So this post should help everyone who finds an error he/she cannot explain to check if there is already an easy solution present.
Please feel free to add to the list.
If there are a lot of replies, just use the search.

First error:
the infamous "MySQL mysql_fetch_row(): supplied argument is not a valid MySQL result resource" error

Full Message:
Warning:  mysql_fetch_row(): supplied argument is not a valid MySQL result resource in code.php on line 4

Example code:
<?php
$sql="SELECT * FROM table WHERE <:,.39iu59u";
$result=mysql_query($sql);
$row=mysql_fetch_row($result);
?>

What it does not mean:
The error is not on line 4!

What it does mean:
The error happened earlier!
The SQL statement had an error in it ("SELECT * FROM table WHERE <:,.39iu59u" is broken).
Because of that, mysql_query($sql) did not return a result, but false.
And because of that, mysql_fetch_row($result); returns an error, because mysql_fetch_row(false) does not make sense.

How to remove the error?
Change the mysql query code above this line like this:
<?php
$sql="SELECT * FROM table WHERE <:,.39iu59u";
$result=mysql_query($sql)
    or die(mysql_error()."<br>nSQL: $sql");
$row=mysql_fetch_row($result);
?>
This will show the error where it happens.

#2 Gustavo el Geranie

    Advanced Member

  • Members
  • PipPipPip
  • 716 posts

Posted 07 July 2003 - 07:21 AM

the unexpected: "parse error, unexpected T_VARIABLE"

Full Message:
Parse error:  parse error, unexpected T_VARIABLE in code.php on line 3

Example code:
<?php
$a='foo'
$b='bar';
?>

What it does not mean:
The error may not be on line 3, but one line above

What it does mean:
In most cases you just forgot the ending ';'.
Generally it means that PHP found a variable where something else should be.

How to remove the error?
Just add the the ending ';'
<?php
$a='foo';
$b='bar';
?>
or any other missing operator.

#3 Gustavo el Geranie

    Advanced Member

  • Members
  • PipPipPip
  • 716 posts

Posted 08 July 2003 - 10:03 AM

the dreaded: headers already sent

Full message:
Warning:  Cannot modify header information - headers already sent by (output started at code.php:1) in code2.php on line 3

Example code:
<?php
// code.php
include "code2.php";
?>
<?php
// code2.php
setcookie("TestCookie", "Test Value");
?>

What does it mean?
Somewhere in your code, you made an output before you used a header function (like setcookie). This can be an empty line in the html code before the opening <?php tag (see code.php), this could be an eror message, this could be an echo statement before setcookie is called.

How to remove the error?
Check you code for any output prior to the header function. Be careful to check also files, where the code may be included.
Pls check the error string, it tells you where the output started:
...headers already sent by (output started at code.php:1)... tells you to look in code.php on line 1.

What are header functions?
If you request a page from a server, the answer of the server comes in two parts.
First comes the header. This part tells the browser what the page contains (image, text, html,...), if it may cache it, if it shell set cookies, and many more.
Second comes the content of the page. This part is called the body.
So if you want to set a cookie, you have to sent this information in the header. PHP waits until the first output is generated. Then the whole header is sent and the first piece of the body. This means as long as there is no output, PHP has the header in memory and you can change it and add to it. But if the first output is made, the header is gone and PHP complains.

#4 Gustavo el Geranie

    Advanced Member

  • Members
  • PipPipPip
  • 716 posts

Posted 14 July 2003 - 04:01 PM

the hideous: my variable is not set, I get no error

Your variable is not correctly set, and you do not get any errors.

Example code:
<?php
function setVar($x){
    $var=$x;
}

setVar(10);
echo $var;
}

Output:
empty

Why?
The variable is set in the function. This variable is local, outside the function it is still empty.

How to find this kind of errors?
Start your code with this statement:
<?php
error_reporting(E_ALL);

function setVar($x){
    $var=$x;
}

setVar(10);
echo $var;

Now PHP complains about all variables that are not set. the code would produce:
Notice:  Undefined variable:  var in code.php on line 9

If you get these notices, you know that your variable is set in a different variable scope. If the variable is set in a function, use global:
<?php
function setVar($x){
    global $var;
    $var=$x;
}

setVar(10);
echo $var;
}

Pls note: before you release your PHP code into a production environement, remove all error_reporting(E_ALL) statements, because they give to much information to an attacker.

#5 Gustavo el Geranie

    Advanced Member

  • Members
  • PipPipPip
  • 716 posts

Posted 04 January 2004 - 08:38 PM

the foreign: unexpected T_PAAMAYIM_NEKUDOTAYIM

Full message:
Parse error:  parse error, unexpected T_PAAMAYIM_NEKUDOTAYIM in code.php

Example code:
<?php
class foo{
    function bar(){
        // do something
    }
}

$foo::bar();
?>

What does it mean?
You cannot use the :: operator on a variable, but only on the name of a class.
Change the example to
foo::bar();
and it works.

What does it mean?
As it turns out, PAAMAYIM NEKUDOTAYIM is hebrew in latin letters and means something like 'double colons'. Probably a PHP developer's insider joke.

#6 Gustavo el Geranie

    Advanced Member

  • Members
  • PipPipPip
  • 716 posts

Posted 17 March 2004 - 11:59 AM

the unkonwn T_: parse error, unexpected T_???

The PHP parser issues often errors complaining about unexpected things starting with T_, like T_CURLY_OPEN or T_OPEN_TAG. It is sometimes difficult to find out, which part of the line relates to the T_???.
There is a page on php.net that explains all T_???, see List of Parser Tokens

#7 YoBro

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 02 February 2005 - 02:58 AM

What about the crazy error you get when looping through an array with foreach, and there is nothing in the array.
EG:
Warning: Invalid argument supplied for foreach() in file.php on line 3
PHP Warning: Invalid argument supplied for foreach() in file.php on line 3

1. <?
2. $var="";
3. foreach($var as $val){
4. echo $val;
5. }
6. ?>

Why doesn't foreach already deal with this issue, it is so dam common, I see it all the time! Might be a search query that returned a zero result etc. It means you have to add an IF statement first, that is just more un-need code if foreach just did nothing instead.
EG:

1. <?
2. $var="";
3. if($var){
4.    foreach($var as $val){
5.       echo $val;
6.    }
7. }
8. ?>

Is that the only way to deal with this issue, or is it always going to be a pain in the ass?

Yobro!

#8 Gustavo el Geranie

    Advanced Member

  • Members
  • PipPipPip
  • 716 posts

Posted 02 February 2005 - 07:52 AM

Hi YoBro,

Quote

What about the crazy error you get when looping through an array with foreach, and there is nothing in the array.
EG:
Warning: Invalid argument supplied for foreach() in file.php on line 3
PHP Warning: Invalid argument supplied for foreach() in file.php on line 3

this error makes sense. You don't get it when the array is empty, but when the variable in foreach is not an array:
<?php
$x=array(); // this is an empty array
foreach ($x as $y){  // no error message
    echo $y;
}
$x='a'; // this is not an array
foreach ($x as $y){  // error message "Invalid argument supplied for foreach()" which is true, because it is not an array
    echo $y;
}
unset($x); // $x is not set -> definitely not an array ;)
foreach ($x as $y){  // error message "Invalid argument supplied for foreach()" which is true, because it is not an array
    echo $y;
}
?>

But it is a good idea to add this error to the list, in a week I will delete the two posts and add a regular entry.

#9 aDesignInteractive

    Newbie

  • Members
  • Pip
  • 0 posts

Posted 23 January 2006 - 02:43 AM

i'm receiving the error unexpected T_BOOLEAN_OR for the following line of code:
 if (($_POST['title']) == '') || (($_POST['desc'])=='') || (($_POST['state'])=='') || (($_POST['date']) == '') { 
although i do wish to find out the cause of this error, and re-write the code properly, i'm also curious about the error itself, and how one should best go about finding the meaning behind these errors when they appear. i've searched php.net, but searching for t_boolean produces zero reults! very odd, don't you think? i'm pretty good w/ finding stuff-- function references and such on php.net, but when it comes to "debugging" errors, i always seem to have difficulty finding the meaning, or the potential cause of these such errors.

thanks!

this is my first visit to phpTalk -- i look forward to being an active member!

#10 Gustavo el Geranie

    Advanced Member

  • Members
  • PipPipPip
  • 716 posts

Posted 23 February 2006 - 03:05 AM

Hi aDesignInteractive,

you are looking for the List of Parser Tokens

#11 aDesignInteractive

    Newbie

  • Members
  • Pip
  • 0 posts

Posted 12 March 2006 - 08:35 PM

Hi there, G. el. G!

Please allow me to begin by saying that it's nice to be a part of another quality PHP Developers' forum. Sure, there are a few out there, but it's difficult to find one which is actually worth visiting, and contributing to the discussion. PHPTalk.com seems to be one of the latter, so kudos to its creators! I should visit more oftne myself. I've put it on my "special" resources list, so next time i have some issues, i'll bring them over for discussion.

regarding the Parser Tokens-- yeah, i'm aware of that particular resource at php.net-- but that's what i meant by debugging issues-- my frustration w/ [quote name='"a.d.i."] I always seem to have difficulty finding the meaning' date=' or the potential cause of these such errors[/quote']. in other words-- do you happen to know of any place where i might find some sort of reference which might, in a way, sort-of "reverse engineer" the Error-- in terms of providing such info as, for example, with the case of my particular T_BOOLEAN error, it was being caused by improperly nested expressions-- i think it would be useful to have a place (and i thought that's kind of what this thread was developing into) where i could look up T_BOOLEAN, and see that it is often caused by issues
A: ___ ;
B: ___ ;
C: often occurs when one of the expressions of the statement is invalid, such as improperly nested parenthesis....___ ;

know what i mean? maybe i'm just being silly, but seems like it could help at least a little. there's not much explanation, but more simply a "listing of" the parser tokens at php.net. am i asking for too much? seriously (not meant to sound sarcastic), is it just me, or is there little knowledge about how to debug the error to be gained from the PHP.NET resource?

#12 Gustavo el Geranie

    Advanced Member

  • Members
  • PipPipPip
  • 716 posts

Posted 12 March 2006 - 08:58 PM

Hi aDI,

this is a good idea. I am going to think about a generic way to find the cause of an error in a line of code. Maybe you want to add your finding about T_BOOLEAN_OR in a small tutorial post below?
Anyway I am looking forward to more posts of you :D





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users