【发布时间】:2014-12-12 02:18:30
【问题描述】:
所以我想做的是在单个页面上尝试一次。这样做的原因是我正在运行我的代码的服务器似乎不喜欢多个尝试捕获。所以我想知道我应该在我的代码中插入什么来捕获数据库连接错误以及查询错误。我的代码对于这个页面来说有点长,所以我尽量缩短它,并且仍然有我所有的 try catch。错误页面工作得很好。我觉得我唯一的问题是扣子的位置
这是我到目前为止所得到的......
try{
$db = @mysqli_connect('xxxx', 'xxxx', 'xxxx', 'xxxx');
if(!$db){
throw new aException(mysqli_connect_error());
}
$strSQL = "SELECT * FROM blah blah blah";
$result = mysqli_query($db, $strSQL);
if(!$result){
throw new bException(mysqli_error($db));
}
$row = mysqli_fetch_array($result);
echo '<select name="customer">';
$custSQL = "select blah blah blah";
$rs=mysqli_query($db,$custSQL);
if(!$rs){
throw new cException(mysqli_error($db));
}
while($row=mysqli_fetch_array($rs)){
if($row['cust_id'] == $customer){
echo '<option selected="selected" value="'.$row[2].'">'.$row['cust_fname']. " " .$row['cust_lname'].'</option>';
}
else{
echo '<option value="'.$row[2].'">'.$row['cust_fname']. " " .$row['cust_lname'].'</option>';
}
}
mysqli_close($db);
unset($db);
}catch(aException $e){
header("Location: error.php?msg=" . $e->getMessage() . "&line=" . $e->getLine());
}
catch(bException $e){
header("Location: error.php?msg=" . $e->getMessage() . "&line=" . $e->getLine());
}
catch(cException $e){
header("Location: error.php?msg=" . $e->getMessage() . "&line=" . $e->getLine());
}
【问题讨论】:
-
您似乎已经拥有它们?
throw new whatever是引发异常的代码。所以catch whatever拿起它。 -
好吧,我还有几个与前 3 个相同的异常,这是我在代码中为异常“致命错误:类 'fException' 不在第 209 行的 /www/virtualhosts/www.wat.com/group4/editorder2.php 中找到“
-
@elephantCoder , 这些是自定义异常吗?
-
您需要使用 fException 扩展 Exception(这是您自己添加到现有代码中的?)
-
不,我只是添加了aException,bException,cException,因为我认为抛出一个新的异常必须命名不同
标签: php exception-handling database-connection try-catch