【问题标题】:Try and Catch error / PHP尝试并捕捉错误/PHP
【发布时间】:2013-07-09 13:38:09
【问题描述】:

我有以下 PHP 代码:

try{ 
    $article_ID =$_GET["articleID"];  
    if(!$article_ID) {
        throw new Exception("Invalid query: ". mysql_error());
    }
    else {
        $select_query = mysql_query("SELECT articleContent, articleTitle From articles WHERE articleID=$article_ID AND typeID=$type_ID");
    }
}
catch(Exception $e) { 
    //echo $e->getMessage();
    $select_query = mysql_query("SELECT articleContent, articleTitle From articles       WHERE typeID=$type_ID");
}
$row = mysql_fetch_assoc($select_query); 
echo '<h1>'.$row['articleTitle'].'</h1>';
echo  $row['articleContent'];

if 语句中的条件 (if(!$article_ID) ) 应该尝试在 get 方法中获取值,如果不能,它将抛出异常并传递给 catch 部分,它工作正常,但我每当遇到问题时,都会在我的网页中看到错误消息 (Notice: Undefined index: articleID on line 6) 为什么?以及如何隐藏此消息?

【问题讨论】:

标签: php mysql try-catch


【解决方案1】:

通知不会引发异常。注意在第一行,因为 $_GET 数组变量中没有“ArticleID”键。

我认为你可以这样做

$articleID = isset($_GET["articleID"]) ? $_GET["articleID"] : '';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-02
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    • 2011-08-07
    相关资源
    最近更新 更多