PHP:我们可以在Try块内返回吗
在某些情况下,我不关心查询是否失败,例如某物===1,在这两种情况下,我都希望PHP:我们可以在Try块内返回吗,php,Php,在某些情况下,我不关心查询是否失败,例如某物===1,在这两种情况下,我都希望返回FALSE或die()等…,因此我执行以下操作: function test($db){ try { $stmt = $db->query("SELECT * FROM table WHERE id=1")->fetch(PDO::FETCH_ASSOC); // If Query fails, PDO itself will throw exception, /
返回FALSE或die()等…
,因此我执行以下操作:
function test($db){
try
{
$stmt = $db->query("SELECT * FROM table WHERE id=1")->fetch(PDO::FETCH_ASSOC);
// If Query fails, PDO itself will throw exception,
// but i also check the value here and throw exception if it's not !== 1
if ($stmt['column'] === 1)
{
throw new PDOException('Wrong Column Info');
}
else
{
return TRUE;
}
}
catch(PDOException $e)
{
return FALSE;
}
}
我的问题是,我使用
抛出新的PDOException
可以吗?还是应该使用异常
而不是PDOException
,然后捕获异常?可以。尝试这个代码应该告诉你这一点
但是,您不需要这样做。这将完成同样的事情。无论哪种方式都可以,我只是指出这一点
function test($db){
try
{
$stmt = $db->query("SELECT * FROM table WHERE id=1")->fetch(PDO::FETCH_ASSOC);
// If Query fails, PDO itself will throw exception,
// but i also check the value here and throw exception if it's not !== 1
if ($stmt['column'] === 1)
{
throw new PDOException('Wrong Column Info');
}
}
catch(PDOException $e)
{
return FALSE;
}
return TRUE;
}
是的,你可以。尝试这个代码应该告诉你这一点 但是,您不需要这样做。这将完成同样的事情。无论哪种方式都可以,我只是指出这一点
function test($db){
try
{
$stmt = $db->query("SELECT * FROM table WHERE id=1")->fetch(PDO::FETCH_ASSOC);
// If Query fails, PDO itself will throw exception,
// but i also check the value here and throw exception if it's not !== 1
if ($stmt['column'] === 1)
{
throw new PDOException('Wrong Column Info');
}
}
catch(PDOException $e)
{
return FALSE;
}
return TRUE;
}
是的,你可以。尝试这个代码应该告诉你这一点 但是,您不需要这样做。这将完成同样的事情。无论哪种方式都可以,我只是指出这一点
function test($db){
try
{
$stmt = $db->query("SELECT * FROM table WHERE id=1")->fetch(PDO::FETCH_ASSOC);
// If Query fails, PDO itself will throw exception,
// but i also check the value here and throw exception if it's not !== 1
if ($stmt['column'] === 1)
{
throw new PDOException('Wrong Column Info');
}
}
catch(PDOException $e)
{
return FALSE;
}
return TRUE;
}
是的,你可以。尝试这个代码应该告诉你这一点 但是,您不需要这样做。这将完成同样的事情。无论哪种方式都可以,我只是指出这一点
function test($db){
try
{
$stmt = $db->query("SELECT * FROM table WHERE id=1")->fetch(PDO::FETCH_ASSOC);
// If Query fails, PDO itself will throw exception,
// but i also check the value here and throw exception if it's not !== 1
if ($stmt['column'] === 1)
{
throw new PDOException('Wrong Column Info');
}
}
catch(PDOException $e)
{
return FALSE;
}
return TRUE;
}
这可以用很多方法来回答。你的方法是可行的,但可以说得更清楚一点 以下是我的看法:
function test($db){
try
{
$stmt = $db->query("SELECT * FROM table WHERE id=1")->fetch(PDO::FETCH_ASSOC);
// If Query fails, PDO itself will throw exception,
// but i also check the value here and throw exception if it's not !== 1
if ($stmt['column'] === 1)
{
throw new PDOException('Wrong Column Info');
}
return true;
}
catch(PDOException $e)
{
error_log("Query failed!");
}
return false;
}
这可以用很多方法来回答。你的方法是可行的,但可以说得更清楚一点 以下是我的看法:
function test($db){
try
{
$stmt = $db->query("SELECT * FROM table WHERE id=1")->fetch(PDO::FETCH_ASSOC);
// If Query fails, PDO itself will throw exception,
// but i also check the value here and throw exception if it's not !== 1
if ($stmt['column'] === 1)
{
throw new PDOException('Wrong Column Info');
}
return true;
}
catch(PDOException $e)
{
error_log("Query failed!");
}
return false;
}
这可以用很多方法来回答。你的方法是可行的,但可以说得更清楚一点 以下是我的看法:
function test($db){
try
{
$stmt = $db->query("SELECT * FROM table WHERE id=1")->fetch(PDO::FETCH_ASSOC);
// If Query fails, PDO itself will throw exception,
// but i also check the value here and throw exception if it's not !== 1
if ($stmt['column'] === 1)
{
throw new PDOException('Wrong Column Info');
}
return true;
}
catch(PDOException $e)
{
error_log("Query failed!");
}
return false;
}
这可以用很多方法来回答。你的方法是可行的,但可以说得更清楚一点 以下是我的看法:
function test($db){
try
{
$stmt = $db->query("SELECT * FROM table WHERE id=1")->fetch(PDO::FETCH_ASSOC);
// If Query fails, PDO itself will throw exception,
// but i also check the value here and throw exception if it's not !== 1
if ($stmt['column'] === 1)
{
throw new PDOException('Wrong Column Info');
}
return true;
}
catch(PDOException $e)
{
error_log("Query failed!");
}
return false;
}
可以使用
PDOException
如果您只是想捕获所有内容并返回false,为什么还要抛出任何东西呢?如果if()测试失败,只需返回false。可以使用PDOException
如果要捕获所有内容并返回false,为什么还要抛出任何东西呢?如果if()测试失败,只需返回false。可以使用PDOException
如果要捕获所有内容并返回false,为什么还要抛出任何东西呢?如果if()测试失败,只需返回false。可以使用PDOException
如果要捕获所有内容并返回false,为什么还要抛出任何东西呢?如果if()测试失败,只需返回false。谢谢,我测试了它,但我想知道它是否正确。我不确定是否有一种正确的方法。有很多方法可以用来完成同样的事情。谢谢,我测试了它,但我想知道它是否正确。我不确定有没有一种正确的方法。有很多方法可以用来完成同样的事情。谢谢,我测试了它,但我想知道它是否正确。我不确定有没有一种正确的方法。有很多方法可以用来完成同样的事情。谢谢,我测试了它,但我想知道它是否正确。我不确定有没有一种正确的方法。有很多方法可以用来完成同样的事情。谢谢,但是即使PDO查询失败了,它也不会返回真值吗?不会,因为抛出异常会停止执行并转到捕获section@Ara,不,try块将在查询发出PDO异常时立即停止。我遇到了一个主要的“WTF no way”当一位同事指出这一点的时候。。。不用担心,伙计:)谢谢,但即使PDO查询失败,这个结果也不会返回真的吗?不会,因为抛出异常将停止执行并转到捕获section@Ara,不,try块将在查询发出PDO异常时立即停止。我有一个重要的“WTF不可能”时刻,一位同事指出了这一点。。。不用担心,伙计:)谢谢,但即使PDO查询失败,这个结果也不会返回真的吗?不会,因为抛出异常将停止执行并转到捕获section@Ara,不,try块将在查询发出PDO异常时立即停止。我有一个重要的“WTF不可能”时刻,一位同事指出了这一点。。。不用担心,伙计:)谢谢,但即使PDO查询失败,这个结果也不会返回真的吗?不会,因为抛出异常将停止执行并转到捕获section@Ara,不,try块将在查询发出PDO异常时立即停止。我有一个重要的“WTF不可能”时刻,一位同事指出了这一点。。。别担心,伙计:)