【问题标题】:How to know the line number from where the exception is coming from in PHP如何知道PHP中异常来自的行号
【发布时间】:2019-09-11 12:51:25
【问题描述】:

我即将完成一个项目,但我注意到我的 error_log 文件中出现错误。当我加载我的 index.php 文件并且在一次重新加载中我得到 21 行错误代码时,我遇到了这个错误。

我已经尝试从 header.php 文件进行调试。最疯狂的事情是在我加载 header.php 文件之前没有任何错误,但是当我在 index.php 文件中调用 header.php 时,我得到了错误。所以我试图通过 PDO 的try 和catch 来捕捉错误。在错误日志文件中,我收到错误消息,因此我将代码从 Select Query: ".$e->getMessage() 更改为 Select Query: ".$e->getFile() 和 Select Query: ".$e->getLine()。但是,这样做我得到了错误所在的行号,而不是它被抛出的行。

我认为我在 databse.php 中的选择查询有问题。以下是我的选择查询代码:

final protected function select($args = array(), $is_die = false){
            try {

        $this->sql = "SELECT ";
        if (isset($args['fields'])) {
            if (is_array($args['fields'])) {
                $this->sql .= implode(', ', $args['fields']);
            } else {
                $this->sql .= $args['fields'];
            }
        } else {
            $this->sql .= " * ";
        }
        $this->sql .= " FROM ";
        if (!isset($this->table) || empty($this->table)) {
            throw new Exception("Table not set");
        }
        $this->sql .= $this->table;

        /*Join Query*/
        if (isset($args['join']) && !empty($args['join'])) {
            $this->sql .= " ".$args['join'];
        }
        /*Join Query*/

        if (isset($args['where']) && !empty($args['where'])) {
            if (is_array($args['where'])) {
                $temp = array();
                foreach ($args['where'] as $column_name => $data) {
                    if (!is_array($data)) {
                        $data = array(
                            'value'     => $data,
                            'operator'  => '=',
                        );
                    }
                    $str = $column_name.' '.$data['operator'].' :'.str_replace('.', '_', $column_name);
                    $temp[] = $str;
                }
                $this->sql .= " WHERE ".implode(' AND ', $temp);
            } else {
                $this->sql .= " WHERE ".$args['where'];
            }
        }

        /*Group*/
        if (isset($args['group_by']) && !empty($args['group_by'])) {
            $this->sql .= " GROUP BY ".$args['group_by'];
        }
        /*Group*/

        /*Order*/
        if (isset($args['order_by']) && !empty($args['order_by'])) {
            $this->sql .= " ORDER BY ".$args['order_by'];
        } else {
            $this->sql .= " ORDER BY ".$this->table.".id DESC";
        }
        /*Order*/

        /*Limit*/
        if (isset($args['limit']) && !empty($args['limit'])) {
            if (is_array($args['limit'])) {
                $this->sql .= " LIMIT ".$args['limit'][0].",".$args['limit'][1];
            } else {
                $this->sql .= " LIMIT ".$args['limit'];
            }
        }
        /*Limit*/
        $this->stmt = $this->conn->prepare($this->sql);
        if (is_array($args['where']) || is_object($args['where'])){

            foreach ($args['where'] as $column_name => $data) {
            $value = is_array($data) ? $data['value'] : $data; //check if passed where statement was an array, fetch value if so
            if (is_int($value)) {
                $param = PDO::PARAM_INT;
            }elseif (is_bool($value)) {
                $param = PDO::PARAM_BOOL;
            }elseif (is_null($value)) {
                $param = PDO::PARAM_NULL;
            }else {
                $param = PDO::PARAM_STR;
            }
            if ($param) {
                $this->stmt->bindValue(":".str_replace('.', '_', $column_name), $value, $param);
            }
        }

        }

        if ($is_die) {
            echo $this->sql;
            debugger($this->stmt);
            debugger($args, true);
        }

        $this->stmt->execute();
        $data = $this->stmt->fetchAll(PDO::FETCH_OBJ);
        return $data;
        } catch (PDOException $e) {

                error_log(
                    date('Y-m-d h:i:s A').", Select Query: ".$e->getMessage()."\r\n"
                    , 3, ERROR_PATH.'/error.log');
                return false;
            } catch (Exception $e) {
                error_log(
                    date('Y-m-d h:i:s A').", General: ".$e->getMessage()."\r\n"
                    , 3, ERROR_PATH.'/error.log');
                return false;
            }
    }

有没有一种可能的方法可以使用 try catch 或任何其他方法来获取抛出错误的文件的行号?

【问题讨论】:

    标签: php exception error-log


    【解决方案1】:

    我刚刚粘贴了代码以在我的选择查询中获取行号和函数名称。

    以下是代码:

    Exception("MySQL error $mysqli->error <br> Query:<br> $sql", $msqli->errno);

    添加上述代码行后,选择查询将如下所示:

    final protected function select($args = array(), $is_die = false){
            try {
    
        $this->sql = "SELECT ";
        if (isset($args['fields'])) {
            if (is_array($args['fields'])) {
                $this->sql .= implode(', ', $args['fields']);
            } else {
                $this->sql .= $args['fields'];
            }
        } else {
            $this->sql .= " * ";
        }
        $this->sql .= " FROM ";
        if (!isset($this->table) || empty($this->table)) {
            throw new Exception("Table not set");
        }
        $this->sql .= $this->table;
    
        /*Join Query*/
        if (isset($args['join']) && !empty($args['join'])) {
            $this->sql .= " ".$args['join'];
        }
        /*Join Query*/
    
        if (isset($args['where']) && !empty($args['where'])) {
            if (is_array($args['where'])) {
                $temp = array();
                foreach ($args['where'] as $column_name => $data) {
                    if (!is_array($data)) {
                        $data = array(
                            'value'     => $data,
                            'operator'  => '=',
                        );
                    }
                    $str = $column_name.' '.$data['operator'].' :'.str_replace('.', '_', $column_name);
                    $temp[] = $str;
                }
                $this->sql .= " WHERE ".implode(' AND ', $temp);
            } else {
                $this->sql .= " WHERE ".$args['where'];
            }
        }
    
        /*Group*/
        if (isset($args['group_by']) && !empty($args['group_by'])) {
            $this->sql .= " GROUP BY ".$args['group_by'];
        }
        /*Group*/
    
        /*Order*/
        if (isset($args['order_by']) && !empty($args['order_by'])) {
            $this->sql .= " ORDER BY ".$args['order_by'];
        } else {
            $this->sql .= " ORDER BY ".$this->table.".id DESC";
        }
        /*Order*/
    
        /*Limit*/
        if (isset($args['limit']) && !empty($args['limit'])) {
            if (is_array($args['limit'])) {
                $this->sql .= " LIMIT ".$args['limit'][0].",".$args['limit'][1];
            } else {
                $this->sql .= " LIMIT ".$args['limit'];
            }
        }
        /*Limit*/
        $this->stmt = $this->conn->prepare($this->sql);
        if (is_array($args['where']) || is_object($args['where'])){
    
            foreach ($args['where'] as $column_name => $data) {
            $value = is_array($data) ? $data['value'] : $data; //check if passed where statement was an array, fetch value if so
            if (is_int($value)) {
                $param = PDO::PARAM_INT;
            }elseif (is_bool($value)) {
                $param = PDO::PARAM_BOOL;
            }elseif (is_null($value)) {
                $param = PDO::PARAM_NULL;
            }else {
                $param = PDO::PARAM_STR;
            }
            if ($param) {
                $this->stmt->bindValue(":".str_replace('.', '_', $column_name), $value, $param);
            }
        }
    
        }
    
        if ($is_die) {
            echo $this->sql;
            debugger($this->stmt);
            debugger($args, true);
        }
    
        $this->stmt->execute();
        $data = $this->stmt->fetchAll(PDO::FETCH_OBJ);
        return $data;
        } catch (PDOException $e) {
    
                error_log(
                    date('Y-m-d h:i:s A').", Select Query: ".$e->getMessage()."\r\n"
                    , 3, ERROR_PATH.'/error.log');
                return false;
            } catch (Exception $e) {
                error_log(
                    date('Y-m-d h:i:s A').", General: ".$e->getMessage()."\r\n"
                    , 3, ERROR_PATH.'/error.log');
                    Exception("MySQL error $mysqli->error <br> Query:<br> $sql", $msqli->errno);
                return false;
            }
    }
    

    更多信息,程序员可以访问这个链接:PHP Manual

    【讨论】:

      【解决方案2】:

      你可以使用try,catch获取异常,对于行号使用__LINE__

      例如

      try {
        /* Your Code */
      } catch (Exception $e) {
         echo __LINE__.$e->getMessage() "\n";
      }
      

      在你的代码里面的catch statememt

      "Line No : " __LINE__.date('Y-m-d h:i:s A')
      

      下面的行号和文件路径使用

      "Line No : " __LINE__." : File Path : ".__FILE__.date('Y-m-d h:i:s A')
      

      【讨论】:

      • 我在哪里粘贴这段代码?我什么也没得到。
      • 它给了我行号,我得到了行号,但是文件名呢,因为我去检查了 index.php、header.php 和 menu.php 第 152 行,有该特定行中没有 php 代码。
      • @Alisha 我已包含文件名,请检查更新的答案
      • 这段代码给出了与 $e->getFile() 和 $e->getLine() 相同的输出
      • @Alisha 最好使用 $e->getFile() 和 $e->getLine() ,如果这些方法存在于对象 $e 中,您可以通过 print_r 看到 $e 的所有方法(get_class_methods($e))
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      相关资源
      最近更新 更多