explanation of PHP code

  • Anonymous
    # August 17, 2014 at 4:12 pm

    I have a good idea of whats going on here but there sections of the following PHP code that i don’t fully understand so it would be great if someone could explain them to me in a direct manner so that its easier to understand.

    try {

        $user = "root";
        $pass = "Jv9524500";
        $db = "Practice";
        $connect = new PDO("mysql:host=localhost; dbname=$db", $user, $pass);
        $connect -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch( PDOException $e ) {
        $e -> getMessage();
        echo "Couldn't connect to the database";


    1. What exactly does the arrow mean in $connect -> setAttribute
    2. Whats going on in this line? } catch( PDOException $e ) {
    3. What exactly is PDO and new PDO?
    # August 17, 2014 at 4:49 pm
    1. The -> arrow is used to call a method on an object; in this case, the setAttribute method on your PDO object.
    2. You use try … catch blocks for exception-based error handling. In basic terms, if there is an exception thrown in your try block, you skip directly to the catch block and do something about it (although, die is not the best solution).
    3. PDO is a php extension used for connecting to various SQL databases, such as MySQL.
    # August 17, 2014 at 4:52 pm

    @TheDoc / @paulie_d, I lost a comment here, if you could kindly restore it … :)

    # August 17, 2014 at 9:40 pm

    Yea that seems to be a huge issue. Posts don’t actually show up.

    # August 18, 2014 at 4:48 am

    @traq — comment restored (was likely automatically moved to spam folder because it contained more than 3 links).

    # August 18, 2014 at 10:32 am

    thanks @senff

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.