Jump to content

How to handle a critical exception involving batabases


ajoo

Recommended Posts

Hi all,

I have a snippet of code below. It connects to a DB and then calls a function findMail() if all is well. 

Code:
<?php
$host = 'localhost';
$user = 'root';
$password = '';
$database = 'test';

// connect
    try
    {
        $con = new mysqli($host,$user,$password,$database);
        if($con->connect_errno > 0) throw new Exception("Server goof up!");    
        mysqli_set_charset($con, "utf8");
    }
    catch(Exception $e){
        $e->getMessage();
    }
    
    if(findMail($con))    echo "<br> Hurray !!";



function findMail($con)
{
    try
    {    
        $query = "SELECT name, email from users";
        $stmt=$con->prepare($query);
        throw new exception("Problem in DB");
        if($stmt->execute())
        {    
            $stmt->bind_result($name, $email);
            while ($stmt->fetch())
                {
                    $email = htmlspecialchars($email);
                    $name = htmlspecialchars($name);
                    echo $name.' ---- '.$email.'<br>';
                }        
        }
    }
    catch(Exception $e){
        $e->getMessage();
    }    
        return true;
}

?>
The function findMail() executes a query and displays the name and email from the DB, returns and prints Hurray. 

If there is an exception thrown in the handling of the DB within the function, then the names and emails from the DB are not echoed, the function returns and prints only Hurray!.

Now if it is critical that findMail() executes successfully for the program to proceed further. ( print hurray on exiting the function) i.e it is important for the call to DataBase not fail, then how should this exception be handled by the program to gracefully exit the program then and there. ( Not print hurray). 

Kindly explain by extending the snippet above. 

Would this be an ideal case for making a call to an error page ( such as 404) on exit to inform the cliet to try again later maybe?

Also how can we ensure that any attempts to reload the previous page using a back key be foiled.

Thanks loads everyone.
Edited by Ch0cu3r
Link to comment
Share on other sites

Hi , Thanks for the response. 

 

Kindly also explain how can I get the code to discontinue execution once an exception occurs.

 

In the snippet, once the exception is thrown in the function, the code after that in that block function is not executed but once back in the main it goes on to echo "Hurray". This is what I want to avoid. So once the exception occurs, I want the code terminated totally, a message displayed to the user on a nice page ( through redirection as you suggested.)

 

Further is it possible to ensure that the user cannot return to the previous page through the backspace or browser buttons.

 

Thanks very much !  

Link to comment
Share on other sites

Hi QuickOldCar, Thanks once again for the response. I was looking for an affirmation on using the exit and header functions since I read that using die(), exit() and header() was bad form and should be avoided and replaced by none else than an exceptions handler. But I guess to terminate as I mentioned I would need to use either exit or die as suggested by you. 

 

Thanks very much. 

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.