the tao
the tao

Reputation: 341

PHP exceptions try and catch

If I were to do the following:

$pds= $pdo->prepare("SELECT * FROM userinfo  WHERE   username=:username AND   password=:password");

$pds->execute(array(':username' => $username, ':password' => $password));

$row = $pds->fetch(PDO::FETCH_ASSOC);

Do I need to put a try {} for each command executed, or will a try block cover the entire code, with a single catch block?

Thanks!

Upvotes: 1

Views: 682

Answers (7)

Riz
Riz

Reputation: 7012

One try/catch block means, if you get exception on first statement, remaining ones will not be executed, which is obvious to do in your case here.

In future if you have a different situation, your choice can be different too.

Upvotes: -1

Your Common Sense
Your Common Sense

Reputation: 158007

Do I need to put a try {} for each command executed, or will a try block cover the entire code,

Neither.

Here goes a set of proper rules right from the real life:

  • use try/catch for the single command or set of commands only in case of
    • you're going to handle the error itself. Frankly, if you have an action to do in case of error: to rollback a transaction, for example.
    • error is non-fatal - to bypass it
  • all other exceptions have to be handled by exception handler, not global try/catch block.

though you can omit the latter one, as PHP has built-in basic handler that works better than one an inexperienced programmer can develop.

Upvotes: 5

hek2mgl
hek2mgl

Reputation: 158250

As all of the methods will potentially throw the same exception: PDOException it could make sense to wrap each call it is own try/catch block. Yes, this is a good idea if you need to react depending on which method throws the exception and don't to parse the exception's errorInfo and/or errorCode (which will be driver dependend)

Upvotes: -1

Dylan N
Dylan N

Reputation: 517

what you would need to do for the query you are doing is:

try{
  $pds= $pdo->prepare("SELECT * FROM userinfo  WHERE   username=:username AND   password=:password");
  $pds->execute(array(':username' => $username, ':password' => $password));
}
catch(PDOException $ex){
  die("Failed to run query: " . $ex->getMessage());
  //Or Echo, or store in a variable to process if you don't want to die()
}

$row = $pds->fetch(PDO::FETCH_ASSOC);

Hope this helps!

Edit: Also, if you want a bit more separation and readability for building a query you can try creating a query parameter array instead of creating the array directly in the execute() function.

$pds = $pdo->prepare("SELECT * FROM userinfo WHERE username=:username AND   password=:password");
$query_params = array(
  ':username' => $username,
  ':password' => $password
);
$result = $pds->execute($query_params);

Upvotes: 0

Peter
Peter

Reputation: 793

A try block will fail catch the first exception that is generated. Therefore it is quite safe to place all 3 statements in the try section.

You can also use multiple catch blocks so that different exception types can be handled differently such as:

 try {
      $pds= $pdo->prepare("SELECT * FROM userinfo  WHERE   username=:username AND   password=:password");
      $pds->execute(array(':username' => $username, ':password' => $password));
      $row = $pds->fetch(PDO::FETCH_ASSOC);
 } catch (PDOException $e) {
      echo 'A pdo exception happened';
 } catch (Exception $e) {
      echo 'A different exception happened';
 }

This helps ensure you can for example clean up after the issue.

Upvotes: 0

IanPudney
IanPudney

Reputation: 6031

You need only to place a try{} block around the all code, and catch it with a single catch{} block. See the php manual for more information.

Upvotes: -1

Gui
Gui

Reputation: 9823

You should definitely study error handling. Also, you should do a little research (at least on stackoverflow) before posting this type of questions.

You can put that code inside a single try block.

try
{
$pds= $pdo->prepare("SELECT * FROM userinfo  WHERE   username=:username AND       password=:password");

$pds->execute(array(':username' => $username, ':password' => $password));

$row = $pds->fetch(PDO::FETCH_ASSOC);
}
catch(Exception $ex)
{


}

Upvotes: -1

Related Questions