簡體   English   中英

調試php。 文件和行

[英]Debugging php. file and line

我正在研究一個較舊的腳本。 有人告訴我腳本使用古老的調試方式(文件和行)。

我發布了一個腳本示例。 我應該使用什么而不是FILE和LINE方法?

mysql類 -

 function error($file, $line) {
            trigger_error("DB error in *<b>{$file}</b>* on line <b>{$line}</b><br />\n " . mysql_error() . "" . @mysql_error($this->dbl), E_USER_ERROR);
        }


      function query($query, $file, $line) {

            $result = mysql_query($query) or $this->error($file, $line);
            $this->num_queries++;

            return $result;
        }

標准查詢 -

$DB->query("SELECT * FROM table"), __FILE__, __LINE__);

實際上,您應該使用debug_backtrace

例如:

function error(){
    $trace = debug_backtrace();
    $file = $trace[1]['file']; // use 1 because you don't want
                               // to include this function!
                               // 2 will be the function which called the function 
                               // which called this function.
    $line = $trace[1]['line'];
    trigger_error("DB error in *<b>{$file}</b>* on line <b>{$line}</b><br />\n " . 
                  // You really should consider using MySQLi instead of MySQL
                  mysql_error() . "" . @mysql_error($this->dbl), E_USER_ERROR);
                  // why do you have two mysql_error calls???
}

您應該考慮從此自定義數據庫包裝器遷移。 最常見的面向對象的庫是MySQLiPDO

這兩個庫都支持錯誤檢查。

MySQLi::query()在出錯時返回false。 您可以通過MySQLi::error()檢索錯誤消息

PDO::query()在出錯時也返回false。 您可以通過PDO::errorInfo()檢索錯誤消息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM