SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot maffp's Avatar
    Join Date
    Jun 2005
    Location
    UK
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy How to stop Scripts from continuing

    Hi!

    Here's an example of my problem... I have several PHP files which call mysql databases and display info. The connection is made like this:

    PHP Code:
    if (!@mysql_select_db('plss_cms')) {
        exit (
    "<p>Unable to connect to the database at this time.</p>\n".
        
    "<p>Please try again later or notify the <a href='mailto:webmaster@domain.co.uk?subject=PLSS Debug: Database Connection Problem'>webmaster</p>\n\n");

    ...

    Then I have lots of mysql calls which all have their own message like "Table not available" if the function doesn't work correctly

    And at the end of each file is my footer:

    PHP Code:
    <?php include('includes/footer.php'); ?>
    The thing is it all works perfectly when the database/server is accessible but when the server is down, my footer isn't parsed and the page DIVs aren't terminated making the page look very messy.

    I would like to just have a message displayed like SERVER NOT ACCESSIBLE or something and the footer displayed. I could, therefore just use 'echo' instead of 'exit' but that would mean that all the rest of the script on the page would be executed even if the server wasn't even working and that would be a waste of server resources.

    Is there a standard way of getting around this?

    Thanks.

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create a template for your page header and footer, leaving the content part empty. You can then do something like this:

    PHP Code:
    $message '';
    if (!
    mysql_connect('args'))
    {
      
    $message "some error";
    }
    else
    {
      
    $message 'some good content';
    }

    // do some more database stuff, remember to change $message based on
    // the error messages you might receive

    include ('header.php');
    /* $message contains the HTML that makes up your content,
     * this can be either the right contents of the page, or an error message
     */
    print '<div id="content">';
    print 
    $message;
    print 
    '</div>';
    include (
    'footer.php'); 
    This way, you never have to exit and all of your HTML code is processed and displayed in the browser. Just make sure all error messages go in the $message string, just as all the actual content.

  3. #3
    SitePoint Evangelist superuser2's Avatar
    Join Date
    Aug 2006
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use the $message thing. It works quite nicely.

  4. #4
    SitePoint Zealot maffp's Avatar
    Join Date
    Jun 2005
    Location
    UK
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Okay, cheers for that ... will give it a go!

    One other thing before I do... I'm using phpdev to test my pages on my home computer. I've deliberately put an error in the server connection script:

    PHP Code:
    $dbcnx = @mysql_connect("localhost""root""wrongpassword");   
    if (!
    $dbcnx) {
        echo (
    "<p>Unable to connect to the server at this time.</p>\n".
        
    "<p>Please try again later or notify the <a href='mailto:webmaster@domain.co.uk?subject=PLSS Debug: Server Connection Problem'>webmaster</a></p>\n\n");

    Then I have the database selection script (shown in last post). I would have thought that because I put the wrong password in the server connect that the database selection would also error but that's not the case! It selects the database and gets the information! Is this just beacuase it's my test server?!

  5. #5
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you could forward the user to a pre built error page.

  6. #6
    SitePoint Evangelist superuser2's Avatar
    Join Date
    Aug 2006
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrwooster View Post
    Or you could forward the user to a pre built error page.
    No, the OP is saying that it's accepting a wrong password.

    Sounds like a caching thing. Restart the server. If that doesn't work, restart your computer. If that doesn't work, let us know.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •