1

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!

0

7 Answers 7

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Seeing that post again since a time. Funny how this was getting upvoted 5 times. The phrase only if you're going to handle the error itself is the only half-way correct statement here. What if you want to create a custom Exception for upper levels, meaning levels which can handle the situation? Something like DupKeyException? That's a common task, as PDO will throw only PDOExceptions. Do you wan't upper levels to handle SQL error states, and driver errors? :D! You are a vandal, I can't see much common sense here.
0

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);

5 Comments

@thetao I wonder why did you choose the most improper way that ever can be.
Did you try this approach yourself?
I do use the try{prepare(); execute();}catch(PDOException $ex){} and it seems to work fine with me, are you implying that PDOexception is the wrong catch to use? Or that the prepare and execute is wrong?
Look. Wrapping every query into dedicated try-catch is a huge waste of time and space. I can't believe someone would write tens and hundreds blocks around every query. Moreover - it is useless. If you get rid of this block from this code, you will have way better error handling.
Okay, that makes sense. Thank you for the insight and I will definitely look in to it more and get a better understanding. Thanks again. :)
0

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.

Comments

-1

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)
{


}

Comments

-1

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.

Comments

-1

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)

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.