2015-12-09 52 views
-2

我被分配了一個大學項目的錯誤處理任務,我無法讓它工作。我需要使用Try catch捕獲SQL錯誤,並將用戶重定向到顯示錯誤的新頁面error.php。我知道這不是處理錯誤的最佳方式,但這正是教授想要的。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語句中,我無法弄清楚。在我開始添加錯誤處理之前,一切正常。

在此先感謝您的幫助。

+0

繼續你的功課,有什麼問題嗎? (使用urlencode的味精) – KiwiJuicer

+2

只要你用'@'來抑制輸出,你就無法捕捉東西 –

+0

@JayBlanchard我的印象是@只是壓制給定函數調用的消息,而不是錯誤本身 – colyerfs

回答

1

你缺少分號:

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

謝謝,清除了如果聲明錯誤 – colyerfs

+0

是的,這就是所有錯誤的代碼! – RiggsFolly

0

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

2)前while($row = mysqli_fetch_array($result)){因爲if(!$result){是假的,那麼爲什麼會得while循環將if(!$result){throw new Exception(mysqli_error($db));}

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()); 
} 
+0

是的,多數民衆贊成在該代碼錯了! – RiggsFolly

+0

謝謝,清除if語句錯誤 – colyerfs

+0

@colyerfs:檢查一次。 –

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()); 
}