11 Replies - 547 Views - Last Post: 06 May 2013 - 12:44 PMRate Topic:

Can't print when I use "or die"

Posted 05 May 2013 - 02:32 PM

I have a php script and with mysql. When I add the 'or die' part to my while loop, the code outside the while loop can't print. but if i take away the 'or die' part, everything prints fine. In both situations, no error message pops up anyway.

But If I eliminate the 'or die' part, i get an error when i upload it to my web server. Below is the code i'm using. Thnx in advance;

Re: Can't print when I use "or die"

Re: Can't print when I use "or die"

Posted 05 May 2013 - 03:24 PM

thatís quite simple. mysql_fetch_assoc() returns false if there are no more rows to fetch, mysql_query() returns false if there is an error in the SQL statement. hence, for one of them it makes sense to die(), for the other not.

Re: Can't print when I use "or die"

Posted 06 May 2013 - 12:44 PM

It's also worth mentioning that or die(mysql_error()) is a very poor way to handle errors. It simply dumps the error onto the screen, in full view of whatever user happens to be requesting the page. Not only does this look extremely bad for you, but it potentially gives out information about the database structure that random users should in no way be given.

A better way is to set up proper error logging (which may include showing errors on-screen on dev servers) and then use functions like trigger_error() or error_log() to log/display the error.

In both cases the script execution will be halted and the error logged. The great thing there is that on development servers this will actually print the error to the screen, but on servers configured for production use, the errors won't show up on screen, only in the logs.