0

Hi All,

Im trying to learn how to use pdo, and am curious to know how i best can deal with allkind off errors. Lets say there is a typo, or im selecting an non existing row etc etc.

In my connection, do I have do set anyting different here:

  Class PDO_CONNECTION extends PDO 
{   
    public function __construct( 
    $DSN, $USER = '', $PASS = '', $DRIVER_OPTIONS = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) )
    {
        // OPRET PDO OBJECT FRA PARENT PDO CLASS :
        parent::__construct( $DSN, $USER, $PASS, $DRIVER_OPTIONS );
     }
}

When I run this query, is this the "best way"to run a query, in a try catch block? Should i allways do that?
(The connection is included in the construct that is also in the class as the method below)

public function create( $cat, $ref, $price, $thumb, $large )
{
    try {
        $stmt = $this->database->prepare("INSERT INTO products 
                                        ( cat, ref, price, img_small, img_large ) 
                                        VALUES 
                                        ( :cat, :ref, :price, :thumb, :large )");
      // Execute :
      if( $stmt->execute(array( 
                    ':cat' => $cat, 
                    ':ref' => $ref, 
                    ':price' => $price, 
                    ':thumb' => $thumb, 
                    ':large' => $large ))) {
                    return true;  
                    } 
                    else {
                        return false;  
                    }

    }
    catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
    }

}

**** Does catch mean, that I can avoid outputting all the ugly err messages, and perhaps log errors in a txt file or whatever else i would prefer? Havent used try/catch before, so forgive my questions :-)

Best, Klemme

4
Contributors
6
Replies
20
Views
4 Years
Discussion Span
Last Post by diafol
0

Does catch mean, that I can avoid outputting all the ugly err messages, and perhaps log errors in a txt file or whatever else i would prefer?

Basically. You can even check the error number and use it to act upon specific errors. There's also a finally statement (PHP >= 5.5 I think), in which you can define additional clean-up code.

0

Sorry, off-topic...

There's also a finally statement (PHP >= 5.5 I think), in which you can define additional clean-up code.

Where can I get 5.5?

0

No - reinstall AFAIK.

Sorry - I'm hijacking. This my last post here.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.