Jump to content

Do I need "connection failed"


FUNKAM35

Recommended Posts

I am trying to improve my php code and speed it up as there were a lot of echos and lines.

Do I need the following snippet or can I delet it, I tried deleting it and page works fine, but di I need it, just thought its another line that could be eliminated???

 

        else

        {

            echo "connection failed.";

        }

Link to comment
Share on other sites

Not that simply echoing things like 'connection failed' is good error handling in the first place, but removing error handling will make your code worse, not improve it.

 

You should be trying to catch as many possible error conditions as you can.

Link to comment
Share on other sites

In your error checking (1), error reporting/logging (2), and error recovery logic (3), you should -

 

1) Always check if something worked or failed,

 

2a) When something fails, output a user message (i.e. the action you tried on this page cannot be performed due to a server error...),

 

2b) When something fails, log all the relevant information about the error so that you know what is happening and you can find and fix the problem.

 

3) Recover from the error gracefully by not executing follow-on code that is dependent on the result of something that has already failed so that you don't produce a string of follow-on errors or send output to the web page that is broken...

 

For the example code that you posted with the "connection failed." logic. Suppose that something happens out of your control, such as your database server is failing intermittently or you are exceeding the number of allowed connections to your database server. You would want to 1) check if your database connection worked or failed, 2a) Display some message to your visitor so that he is not faced with a blank page, 2b) log everything about the error, and 3) recover from the error by displaying the remainder of your page correctly but not executing all the code that was dependent on that database connection.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.