PHP错误处理 - 重定向

时间:2015-12-09 14:49:50

标签: php mysql error-handling

我被分配了一个大学项目的错误处理任务,我无法让它工作。我需要使用Try catch捕获SQL错误,并将用户重定向到显示错误的新页面error.php。我知道这不是处理错误的最佳方式,但这是教授想要的。

以下是我尝试捕获错误的其中一个页面的示例:

<?php
    $orderid = $_REQUEST['orderid'];
    connectDB();  // database connections are defined in connect_to_DB.php  

    try{
        $strSql2 = "SELECT `order_date`, `status_id`, `emp_id`,`cust_id` FROM `salesorder` WHERE `order_id`='" .$orderid. "'" ;

        $result = @mysqli_query($db, $strSql2) // or die("SQL error: " . mysqli_error());

        if($row = mysqli_fetch_array($result)){
            $orderDate = $row["order_date"];
            $orderStatus=$row["status_id"];
            $empid = $row["emp_id"];
            $custid=$row["cust_id"];

            if(!$result){
                throw new Exception(mysqli_error($db));  
            }

            mysqli_free_result($result);  // Always release the recordset memory resources
            mysqli_close($db);  // Always close the database connection 

            connectDB();
            $sqlcount = "SELECT COUNT(order_id) AS numrows FROM orderitem WHERE order_id=".$_REQUEST['orderid'];
            $resultcount = @mysqli_query($db,$sqlcount) // or die ("SQL error: ".mysqli_error());

            if($rowcount= mysqli_fetch_array($resultcount)){
                $orderamount = $rowcount['numrows'];
                if(!$resultcount){
                    throw new Exception(mysqli_error($db));  
                }

                mysqli_free_result($resultcount);
                mysqli_close($db);
            }
        }
    } catch (Exception $e){
        // redirect to a php error page
        header("Location: error.php?msg=" . $e->getMessage() . "&line=" . $e->getLine());
    }

这是错误页面error.php上的代码:

<? 
    print "Error message: " . $_GET["msg"];
    if(isset($_GET["line"])){
        print " - line number: " . $_GET["line"]; 
    }

抱歉意外删除了这篇文章的结尾...现在第66行if($row = mysqli)上的if语句出现了错误,我无法清除。在我开始添加错误处理之前,一切正常。

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

您缺少分号:

$result = @mysqli_query($db, $strSql2);

答案 1 :(得分:0)

1)将if($row = mysqli_fetch_array($result)){更改为while($row = mysqli_fetch_array($result)){

2)将if(!$result){throw new Exception(mysqli_error($db));}放在while($row = mysqli_fetch_array($result)){之前因为if(!$result){为假,所以它为什么会循环播放。

3)不要进行多重连接。

<?php
$orderid = $_REQUEST['orderid'];
connectDB();  // database connections are defined in connect_to_DB.php  

try{
    $strSql2 = "SELECT `order_date`, `status_id`, `emp_id`,`cust_id` FROM `salesorder` WHERE `order_id`='" .$orderid. "'" ;

    $result = @mysqli_query($db, $strSql2) // or die("SQL error: " . mysqli_error());

    if(!$result){
       throw new Exception(mysqli_error($db));  
    }

    while($row = mysqli_fetch_array($result)){
            .
            .
            // Remove this close and open connection.
            //mysqli_close($db);  // Always close the database connection 
            //connectDB();

    }
} catch (Exception $e) {
    header("Location: error.php?msg=" . $e->getMessage() . "&line=" . $e->getLine());
}

答案 2 :(得分:0)

你的代码中存在很多错误,我只是提供这个作为你继续做作业的更好的起点

<?php
$orderid = $_REQUEST['orderid'];

// connect only once per script
// useful comment here would be 

// connectDB() set the connection handle onto $db in the global scope
// not good practice but at least now we know
connectDB();  

try{
    $sql = "SELECT `order_date`, `status_id`, `emp_id`,`cust_id` 
            FROM `salesorder` 
            WHERE `order_id`='$orderid'" ;

    $result = mysqli_query($db, $sql);
    // test statuses when you receive them not sometime later
    // after attempting to use a potentially useless mysqli::result object

    if(!$result){
        throw new Exception(mysqli_error($db));  
    }


    $row = mysqli_fetch_array($result);

    $orderDate    = $row["order_date"];
    $orderStatus  = $row["status_id"];
    $empid        = $row["emp_id"];
    $custid       = $row["cust_id"];


    $sql = "SELECT COUNT(order_id) AS numrows 
            FROM orderitem 
            WHERE order_id={$_REQUEST['orderid']}";

    $resultcount = mysqli_query($db,$sqlcount);

    // again test status now not later
    if(!$resultcount){
        throw new Exception(mysqli_error($db));  
    }

    // only one result row so no loop necessary
    $row = mysqli_fetch_array($resultcount)){

    $orderamount = $row['numrows'];

} catch (Exception $e){
    // redirect to a php error page
    header("Location: error.php?msg=" . $e->getMessage() . "&line=" . $e->getLine());
}