success OR DIE is there an alternate solution to dying?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

danselstudios
Forum Newbie
Posts: 24
Joined: Sat Jun 03, 2006 10:47 am
Location: Corona, CA

success OR DIE is there an alternate solution to dying?

Post by danselstudios »

ok so i'm making this login script that checks for alot of errors.
my script goes something like this.

Code: Select all

<?php
if( isset(post'submitted'){
     $errors = array();
     if( empty(post'username'){
          $errors[] = 'you forgot the username';
     if( empty(post'password'){
          $errors[] = 'you forgot the password';
     if( empty($errors) ){
          mysql_connect  'blah blah'  or die  'oops oops';
          mysql_select_db  'data'   or die  'sorry sorry';
          mysql_query 'do this do this'   or die  'cant do can't do';
          if( $result ){
                success!!!!!;
          mysql_free_result '$result';
          mysql_close '$link';
so on and so on.
id like to add more errors to the $errors ARRAY if mysql doesn't connect, mysql doesn't select db, and mysql doesn't successfully query.

for the script above,,,what happens if mysql_connect doesn't CONNECT???? does the page continue loading???
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

die() stops execution of the script.

You could try "or echo". I thought that I tried it once and it worked?
jonra
Forum Newbie
Posts: 22
Joined: Thu May 25, 2006 9:35 am
Location: Iowa
Contact:

Post by jonra »

I believe the page would cease to load. Any HTML should render though, unless of course it was encapsulated within PHP tags in the page.

I honestly don't like the use of 'or die' at all. What I will do is on any DB-driven page or form, I will create a small table somewhere in plain view to use for output. Then, instead of using 'or die' I'll use "if(!$dbConnect){ $error = true }" style coding for any connections or queries. That way, you can have the page load and still give friendly errors. So in your actual page, if the DB fails, you'd have something like:

Code: Select all

if($error){
     echo "<table><tr><td>We're sorry, there was a problem connecting to the database. Please try again.</td></tr>";
}
A lot of people like 'die', but I kind of put it in the same boat as 'break' - It's probably personal preference, but I never like to have a situation where I've told my application to simply stop processing no matter what happens. Maybe just me?
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

jonra wrote:I believe the page would cease to load. Any HTML should render though, unless of course it was encapsulated within PHP tags in the page.

I honestly don't like the use of 'or die' at all. What I will do is on any DB-driven page or form, I will create a small table somewhere in plain view to use for output. Then, instead of using 'or die' I'll use "if(!$dbConnect){ $error = true }" style coding for any connections or queries. That way, you can have the page load and still give friendly errors. So in your actual page, if the DB fails, you'd have something like:

Code: Select all

if($error){
     echo "<table><tr><td>We're sorry, there was a problem connecting to the database. Please try again.</td></tr>";
}
A lot of people like 'die', but I kind of put it in the same boat as 'break' - It's probably personal preference, but I never like to have a situation where I've told my application to simply stop processing no matter what happens. Maybe just me?
die(); is alright for testing purposes. However, on a live site, I prefer using an array to echo
predefined error messages - not only to keep the layout intact but also to be able to call a mail function later on.

aerodromoi
danselstudios
Forum Newbie
Posts: 24
Joined: Sat Jun 03, 2006 10:47 am
Location: Corona, CA

Post by danselstudios »

JONRA you make a very good point.

Daedalus, thanks for the information. i definately do not want my pages to completely stop and return nothing for the user to see...

i'll try ' or echo '....
jonra
Forum Newbie
Posts: 22
Joined: Thu May 25, 2006 9:35 am
Location: Iowa
Contact:

Post by jonra »

aerodromoi wrote:
die(); is alright for testing purposes. However, on a live site, I prefer using an array to echo
predefined error messages - not only to keep the layout intact but also to be able to call a mail function later on.

aerodromoi
Hmm, the array is an approach I've never considered. Are you using a multi-dimensional array for this? I thought at times of storing different error messages, but could never find a convenient way to access them based on what condition occurrs.
danselstudios
Forum Newbie
Posts: 24
Joined: Sat Jun 03, 2006 10:47 am
Location: Corona, CA

Post by danselstudios »

aerodromoi - could you tell me what you put after lets say this:

Code: Select all

$link = mysql_connect( '$host', '$user', '$pass' )   // what if it doesn't connect??
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

danselstudios wrote:JONRA you make a very good point.

Daedalus, thanks for the information. i definately do not want my pages to completely stop and return nothing for the user to see...

i'll try ' or echo '....
But keep in mind that echoing an error message does not stop the script -
so you'll have to check whether variables have actually been pulled out of the db, for example.


aerodromoi
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

The only time that I think I only use exit() (i.e. die()) is after sending a header to redirect. Otherwise I always proceed with building a response to send to the user that hopefully gives some meaningful information. It most cases like this, the error message says to contact an administrator because obviously something is very wrong. I may also include a more specific/technical error message for the user to pass on to support. I have also had cases where I had the script email the error messages to me, mainly in cases where there was a potential external source for the error.
(#10850)
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

danselstudios wrote:aerodromoi - could you tell me what you put after lets say this:

Code: Select all

$link = mysql_connect( '$host', '$user', '$pass' )   // what if it doesn't connect??
either:

Code: Select all

$link = @mysql_connect($host, $user, $pass) or $error['mysql_connect']=mysql_error();
or

Code: Select all

$link = @mysql_connect($host, $user, $pass);
if(!$link) $error['mysql_connect']=mysql_error();
aerodromoi
danselstudios
Forum Newbie
Posts: 24
Joined: Sat Jun 03, 2006 10:47 am
Location: Corona, CA

Post by danselstudios »

Let's say I do it this way:

Code: Select all

if( empty($errors) ){
     $link = mysql_connect( '$host', '$user', '$pass' );
     if( !$link ){
          error('Could not connect to host');
          return;
     }
     $db = mysql_select_db( '$dbname' );
     if( !$db ){
          error('Could not connect to database.');
          return;
     }
     $username = trim($_POST['username']);
     $userpass =  trim($_POST['userpass']);
     $query = 'SELECT * FROM $table WHERE username = "$username" AND userpass = "$userpass"';
     $result = mysql_query( $query );
     if( !$result ){
           error('You are not registered.');
           return;
     }
     else{
            echo Success();
     }
     mysql_free_result( $result );
     mysql_close( $link );
What happens when i use 'return' LIKE that??? where does it return to??? the main if clause has an else clause, does it execute the ' else ' clause?????????
jonra
Forum Newbie
Posts: 22
Joined: Thu May 25, 2006 9:35 am
Location: Iowa
Contact:

Post by jonra »

If it was in a function you could return an error message or something... I'm not sure what you'd be returning in this example. I don't think the else would run, but I only use return to actually return data, so am not sure what would happen exactly. Best way to find out is to test :D
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Since we're talking about error handling and the die() function.

I thought it would be good to remind everyone that whenever you are dealing with a function that will cause the page to die on error, remember to prepend the @ sign to the function name.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Error suppression might not be the best thing to do if you actually want to see what the errors are and what they are thowing. There are other ways to hush errors (although I would recommend coding to prevent errors rather than suppressing them).
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

Daedalus- wrote:Since we're talking about error handling and the die() function.

I thought it would be good to remind everyone that whenever you are dealing with a function that will cause the page to die on error, remember to prepend the @ sign to the function name.
It wouldn't make sense otherwise ;)
btw: That's why I included it in my examples...
Everah wrote:Error suppression might not be the best thing to do if you actually want to see what the errors are and what they are thowing. There are other ways to hush errors (although I would recommend coding to prevent errors rather than suppressing them).
That's why I've proposed storing the error message in an array - so that it can be used later (eg. mail();)

aerodromoi
Post Reply