首页 > 解决方案 > PHP 致命错误:我是 php 新手。在我的 wordpress 网站中出现这个未知错误

问题描述

由于我是 PHP 新手,因此我对以下错误不太了解。有人告诉我将我的 PHP 版本 7.2 降级到 7.0,但这个问题仍然存在并导致 500 内部错误。

未捕获的错误:当不在 /home/e61fff4mjhlu/public_html/tcs2/wp-content/plugins/Addquestions/Model/Exam.php:56 的对象上下文中时使用 $this

下面是第 56 行代码在 Exam.php 中的样子:

$sql="SELECT `ExamMaxquestion`.`subject_id`,`ExamMaxquestion`.`max_question` from `".$this->wpdb->prefix."emp_exam_maxquestions` AS `ExamMaxquestion` where `ExamMaxquestion`.`exam_id`=".$id;

Exam.php 完整代码:-

 <?php
$dir = plugin_dir_path(__FILE__);
class Exam extends ExamApps
{
    public function validate($post)
    {
        $gump = new GUMP();
        $post=$this->globalSanitize($post); // You don't have to sanitize, but it's safest to do so.
        $gump->validation_rules(array(
                'name'    => 'required|alphaNumericCustom',
                'passing_percent'    => 'required|numeric',
                'duration'    => 'required|numeric',
                'attempt_count'    => 'required|numeric',
                'start_date'    => 'required|date',
                'end_date'    => 'required|date'

                ));
        $gump->filter_rules(array(
                'name' => 'trim'
                ));
        $validatedData = $gump->run($post);
        GUMP::set_field_name("name", "Group Name");
        return array('validatedData'=>$validatedData,'error'=>$gump->get_readable_errors(true));
    }    
    public function examStats($id)
    {
        $sql="SELECT `Exam`.`id`,`Exam`.`name`,`Exam`.`start_date`,`Exam`.`end_date`,`Exam`.`passing_percent` from `".$this->wpdb->prefix."emp_exams` AS `Exam` INNER JOIN `".$this->wpdb->prefix."emp_exam_groups` AS `ExamGroup` ON (`Exam`.`id`=`ExamGroup`.`exam_id`) WHERE `Exam`.`status`='Closed' and `Exam`.`id`=".$id;
        $this->autoInsert->iFetch($sql,$examvalue);
        $examStats=array();
        $examStats['Exam']['id']=$examvalue['id'];
        $examStats['Exam']['name']=$examvalue['name'];
        $examStats['Exam']['start_date']=$examvalue['start_date'];
        $examStats['Exam']['end_date']=$examvalue['end_date'];
        $examStats['OverallResult']['passing']=(float) $examvalue['passing_percent'];
        $examStats['OverallResult']['average']=(float) $this->studentAverageResult($examvalue['id']);
        $examStats['StudentStat']['pass']=$this->studentStat($examvalue['id'],'Pass');
        $examStats['StudentStat']['fail']=$this->studentStat($examvalue['id'],'Fail');
        $examStats['StudentStat']['absent']=$this->examTotalAbsent($examvalue['id']);
        return$examStats;
    }
    public function examAttendance($id,$type)
    {
        $examStats=array();
        $examStats=$this->studentStat($id,$type,'all');
        return$examStats;
    }
    public function examAbsent($id)
    {
      $examStats=array();
      $examStats=$this->examTotalAbsent($id,'all');
      return$examStats;
    }
    public function totalMarks($id)
    {
        $limit=0;
        $sql="SELECT `ExamMaxquestion`.`subject_id`,`ExamMaxquestion`.`max_question` from `".$this->wpdb->prefix."emp_exam_maxquestions` AS `ExamMaxquestion` where `ExamMaxquestion`.`exam_id`=".$id;
        $this->autoInsert->iWhileFetch($sql,$examMaxQuestionArr);
        $totalMarks=0;
        if($examMaxQuestionArr)
        {
          foreach($examMaxQuestionArr as $value)
          {
            $quesNo=$value['max_question'];
            $subjectId=$value['subject_id'];
            if($quesNo==0)
            $limit=" ";
            else
            $limit=' LIMIT '.$quesNo;
            $sqlExamQuestion="select sum(`marks`) AS `total_marks` from (select `Question`.`marks` FROM `".$this->wpdb->prefix."emp_exam_questions` AS `ExamQuestion` Inner JOIN `".$this->wpdb->prefix."emp_questions` AS `Question` ON (`ExamQuestion`.`question_id`=`Question`.`id`) WHERE `ExamQuestion`.`exam_id`=".$id." AND `Question`.`subject_id`=".$subjectId.$limit.") AS `ExamQuestion`";
            $this->autoInsert->iFetch($sqlExamQuestion,$totalMarksArr);
            $totalMarks=$totalMarks+$totalMarksArr['total_marks'];
        }
    }
    else
    {
        $sql="SELECT SUM(`Question`.`marks`) AS `total_marks` from `".$this->wpdb->prefix."emp_exam_questions` AS `ExamQuestion` Inner JOIN `".$this->wpdb->prefix."emp_questions` AS `Question` ON (`Question`.`id`=`ExamQuestion`.`question_id`) where `ExamQuestion`.`exam_id`=".$id;
        $this->autoInsert->iFetch($sql,$totalMarksArr);
        $totalMarks=$totalMarksArr['total_marks'];
    }    
    return$totalMarks;
  }    
}
?>

标签: phpmysqlwordpress

解决方案


$this在对象中用于引用自身。看起来您正试图在对象之外使用它。在这种情况下,您应该像这样引用全局 wpdb 前缀:

global $wpdb;
$sql = "SELECT ExamQuestion.subject_id, ExamQuestion.max_question FROM ".$wpdb->prefix."emp_exam_maxquestions AS ExamQuestion WHERE ExamMaxquestion.exam_id=".$id;

推荐阅读