-3

i try to learn PHP OOP, just try to make simple script which print down all users from database, my code looks like:

<?php
/*  start config */
    define( "DB_HOST", "mysql:host=localhost;dbname=test" );    
    define( "DB_USER", "root" );
    define( "DB_PASS", "" );

/* end config */
try{
$connection = new PDO("DB_HOST","DB_USER","DB_PASS");
$connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM users";
$stmt = $connection->prepare( $sql );
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
    while( $row = $stmt->fetch()) {
           echo $row['user'];
    }
}catch{
  (PDOExeption $e);{
  echo $e->getMessage();
  }
}                    

?>

I try to follow the example code and in my eyes code looks good but it throw this error: Parse error: syntax error, unexpected '{', expecting '(' in D:\xampp\htdocs\xampp\oop\andurit.php on line 18

Why? i am pretty sure that { have to be here to start that catch so WHY its not working, i hope for some explain what i am doing wrong :)

thanks you all

tereško
  • 58,060
  • 25
  • 98
  • 150
  • 1
    You have a semi-colon here that shouldn't be there: `(PDOExeption $e);{` –  Jul 09 '14 at 20:13
  • possible duplicate of [PHP Parse/Syntax errors and how to solve them](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Jocelyn Jul 10 '14 at 00:05

4 Answers4

1

You don't need the first { or ;

<?php
    /*  start config */
        define( "DB_HOST", "mysql:host=localhost;dbname=test" );    
        define( "DB_USER", "root" );
        define( "DB_PASS", "" );

    /* end config */
    try{
    $connection = new PDO(DB_HOST,DB_USER,DB_PASS);
    $connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $sql = "SELECT * FROM users";
    $stmt = $connection->prepare( $sql );
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $stmt->execute();
        while( $row = $stmt->fetch()) {
               echo $row['user'];
        }
    }catch(PDOException $e){
      echo $e->getMessage();

    }                    

    ?>
A.O.
  • 3,733
  • 6
  • 30
  • 49
  • First i want to thank you for your answer, unfortunately if i use it it call this error" `Parse error: syntax error, unexpected '}' in D:\xampp\htdocs\xampp\oop\andurit.php on line 21` So it dont know where } is starting if i understand it right – user3809565 Jul 09 '14 at 20:15
  • Just remove the '}' after the echo $e->getMessage() and that code will work. – gview Jul 09 '14 at 20:17
  • Looks i am tard or something , but this throw me error : `Fatal error: Uncaught exception 'PDOException' with message 'invalid data source name' in D:\xampp\htdocs\xampp\oop\andurit.php:9 Stack trace: #0 D:\xampp\htdocs\xampp\oop\andurit.php(9): PDO->__construct('DB_HOST', 'DB_USER', 'DB_PASS') #1 {main} thrown in D:\xampp\htdocs\xampp\oop\andurit.php on line 9` – user3809565 Jul 09 '14 at 20:18
  • 1
    You had `PDOxception` spelled incorrectly – A.O. Jul 09 '14 at 20:34
  • @user3809565 Make sure your credentials to your database are correct. – eluong Jul 09 '14 at 20:37
1
  (PDOExeption $e);{
                  ^----remove this semi-colon
  echo $e->getMessage();

A try/catch looks like:

try {
   .... stuff happens
} catch (something $var) {
   ... do stuff
}

Your extra ; is terminating the try/catch, before any of the actual catch code is seen by the parser.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • yea mate , thanks for answer, i follow A.O. advise (which is basicly thee same as your i just didnt understand it from start) and it dont throw same error again, unfortunately it says now: `Fatal error: Uncaught exception 'PDOException' with message 'invalid data source name' in D:\xampp\htdocs\xampp\oop\andurit.php:9 Stack trace: #0 D:\xampp\htdocs\xampp\oop\andurit.php(9): PDO->__construct('DB_HOST', 'DB_USER', 'DB_PASS') #1 {main} thrown in D:\xampp\htdocs\xampp\oop\andurit.php on line 9` Is there a chance you can help me with that? – user3809565 Jul 09 '14 at 20:25
1
}catch{
  (PDOExeption $e);{
  echo $e->getMessage();
  }
}

This should be:

} catch(PDOException $e) {

Also, I think you want your connection to look like this:

$connection = new PDO(DB_HOST, DB_USER, DB_PASS); // no quotes around constants
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • yea mate , thanks for answer, i follow A.O. advise (which is basicly thee same as your) and it dont throw same error again, unfortunately it says now: `Fatal error: Uncaught exception 'PDOException' with message 'invalid data source name' in D:\xampp\htdocs\xampp\oop\andurit.php:9 Stack trace: #0 D:\xampp\htdocs\xampp\oop\andurit.php(9): PDO->__construct('DB_HOST', 'DB_USER', 'DB_PASS') #1 {main} thrown in D:\xampp\htdocs\xampp\oop\andurit.php on line 9` – user3809565 Jul 09 '14 at 20:23
  • check the spelling of PDOException – Jeff Lambert Jul 09 '14 at 20:36
  • check my update, take those quotes out and see if it solves your connection issue. – Jeff Lambert Jul 09 '14 at 20:38
0

"Catch" statement is off. Should be something like this:

} catch (PDOException $e) {
echo $e->getMessage();
}
David Wyly
  • 1,671
  • 1
  • 11
  • 19
  • Thanks for your answer mate, it was helpfull and actauly work, if i could mark all of you or give u all thummb up i will do it:) – user3809565 Jul 09 '14 at 21:10