php - 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;
}
}
?>
解决方案
$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;
推荐阅读
- python - Woocommerce 订单导出 API 为同一订单 Python 的每个项目单独的行
- docker - 当包收到(安全)更新时重建 Docker 镜像
- c - 你可以在 C 中将两个这样的二维数组相乘吗?
- ios - 如何关闭多个当前视图控制器?
- git - SSH 密钥问题 Git-CPanel - 权限被拒绝(公钥)。致命:无法从远程存储库中读取
- group-policy - 组策略在一些加入域的客户端计算机上完全失败
- java - Java 8:如何为 POST 请求分块多部分文件
- reactjs - TypeError:无法读取 ReactJS Mapbox-GL 中未定义的属性“easeTo”
- git - 尝试从 git 中删除大文件失败
- javascript - 如何将数组存储在javascript文件中