Php、PDO、Sql、Try-Catch、连接
例如,当我使用try-catch块时:Php、PDO、Sql、Try-Catch、连接,php,sql,pdo,Php,Sql,Pdo,例如,当我使用try-catch块时: try{ require_once('../php/connect.php'); $dbh = db::getInstance(); // What i am trying to understand should i close the connection here before the header? header("Location: x.php"); } } catch(PDOException $e
try{
require_once('../php/connect.php');
$dbh = db::getInstance();
// What i am trying to understand should i close the connection here before the header?
header("Location: x.php");
}
} catch(PDOException $e){
require_once('err.php');
}
$dbh = null; // Or should i close the connection here?
“解释器”是否在“5”行之后,谢谢大家,祝大家愉快。由于您没有在
header()之后使用退出(即),您的代码将继续执行
因此,无论您是在第4行还是第10行输入。此外,正如prodigitalson所指出的,您不需要显式地关闭连接。您根本不需要关闭连接。它将在php退出时关闭。但是,您没有在标题
之后调用退出
你的代码没有成功,如果db连接成功,你为什么不做任何事情就重定向????如果使用header,我认为您会调用它重定向到错误页面(即在catch
块中)。这根本没有意义。您正在设置$dbh,如果未引发异常,您将发送一个位置头,该头将退出脚本。我不明白你的问题。你知道你也犯了双重错误吗?如果我的代码没有任何意义,我很抱歉,因为它只是代码的一部分,在与标题的连接之间,您可以在按下“发送”键时询问问题。连接下方的代码将数据输入数据库,标题将引导您回答问题。如果我没有弄错,标题将等待脚本完成,然后在您退出时将用户带到x.php位置;在你杀死剩下的脚本/连接的头之后,我确实看到了连接链接,php手册在catch块中使用了die(),如果你能告诉我,catch块是否会自动关闭连接?因为如果是,我知道手册中使用了die(),所以代码不会继续被解释,或者,如果catch无法关闭连接,die()将关闭连接并停止要解释的代码。在header()之后使用exit
是正确的。否,catch
不会自动关闭连接。当PHP退出时,连接会自动关闭-正常或使用退出
,死亡
等显式关闭。