Alex Polo
Alex Polo

Reputation: 905

How to not die?

I want something like this:

or function() { echo mysql_error().'<br>'; } // doesn't work

Instead of:

or die(mysql_error());

I want to continue the execution of the script, not to "die". Can't I use the anonymous function that way? I just woudn't like to name it.

EDIT/TIP: For creating anonymous functions, there's a lambda-style function called create_function...

Upvotes: 1

Views: 205

Answers (3)

Paul Hoffer
Paul Hoffer

Reputation: 12906

If you place an @ before your function, it will suppress errors.

$example = @mysql_query(....);

Although it is generally a bad idea to use this.

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157896

or print  mysql_error();

though printing it out is almost bad as dying.
In the perfect world it would be

$res = mysql_query($query) or trigger_error(mysql_error().$query);

always do it that way. You'll get not only filename and line number (which is MIGHTY helpful when you have plenty of queries on the page) but also will keep sensitive information secret on the production server, where you will turn displaying errors off and logging - on.

Upvotes: 4

Artefacto
Artefacto

Reputation: 97835

function() { echo mysql_error().'<br>'; }

defines a function, it does not call it. In order to call it, you must do

$a =  function() { echo mysql_error().'<br>'; };
$a();

or use call_user_func, e.g.:

false or call_user_func(function() { echo "hi".'<br>'; });

Of course, you might as well write;

false or printf("hi<br />");

echo does not work because it's not a function; it's a language construct. (doesn't work for some other syntactic reason, since print is also a language construct and it does work).

Upvotes: 2

Related Questions