【发布时间】: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) 为什么?以及如何隐藏此消息?
【问题讨论】:
-
这个问题问了很多次,用
isset($_GET["articleID"]) -
Please, don't use
mysql_*functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果选择 PDO,here is a good tutorial