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

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.

Member Avatar for diafol

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?

Member Avatar for diafol

Aha! Thanks, I was looking here: http://php.net/releases/index.php

Garrhh! XAMPP still at PHP 5.4.16. Will have to use binaries :(

Is there away of updating PHP in XAMPP?

Member Avatar for diafol

No - reinstall AFAIK.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.