1

如果我要执行以下操作:

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

我是否需要为每个执行的命令添加一个 try {},或者一个 try 块是否会用一个 catch 块覆盖整个代码?

谢谢!

4

7 回答 7

5

我是否需要为每个执行的命令添加一个 try {},或者一个 try 块是否会覆盖整个代码,

两者都不。

这里有一套来自现实生活的适当规则:

  • 在以下情况下对 单个命令或一组命令使用 try/catch
    • 你将自己处理错误。坦率地说,如果您在发生错误时需要执行操作:例如,回滚事务。
    • 错误是非致命的 - 绕过它
  • 所有其他异常都必须由异常处理程序处理,而不是全局 try/catch 块。

尽管您可以省略后一个,因为 PHP 具有内置的基本处理程序,它比没有经验的程序员可以开发的更好。

于 2013-06-09T04:17:58.937 回答
0

你需要为你正在做的查询做的是:

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

希望这可以帮助!

编辑:此外,如果您想要更多的分离和可读性来构建查询,您可以尝试创建查询参数数组,而不是直接在 execute() 函数中创建数组。

$pds = $pdo->prepare("SELECT * FROM userinfo WHERE username=:username AND   password=:password");
$query_params = array(
  ':username' => $username,
  ':password' => $password
);
$result = $pds->execute($query_params);
于 2013-06-09T00:54:08.063 回答
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.

于 2013-06-09T00:48:55.370 回答
-1

一个 try/catch 块意味着,如果您在第一条语句上遇到异常,则不会执行剩余的语句,这在您的情况下很明显。

将来如果你有不同的情况,你的选择也会不同。

于 2013-06-09T01:17:23.260 回答
-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)
{


}
于 2013-06-09T00:48:12.777 回答
-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.

于 2013-06-09T00:48:34.520 回答
-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)

于 2013-06-09T00:49:24.607 回答