由于我是PHP的新手,所以我对以下错误不太了解。 有人告诉我将我的PHP版本7.2降级到7.0,但是仍然存在此问题并导致500内部错误。
未捕获的错误:不在对象上下文中时使用$ this /home/e61fff4mjhlu/public_html/tcs2/wp-content/plugins/Addquestions/Model/Exam.php:56
这是Exam.php中第56行代码的样子:
$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;
}
}
?>
答案 0 :(得分:0)
$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;
答案 1 :(得分:0)
$ this似乎是指表的wordpress数据库前缀。通常,您的表将被命名为xxx_tablename-xxx是您在安装WP时定义的前缀。 $ this作为变量通常在已在类中定义了$ this变量的函数中找到。如果您从某个函数复制了代码,请返回并找出表名的传递方式,然后将$ this变量更改为表名前缀-或仅使用前缀对其进行硬编码:
FROM `myprefix_emp_exam_maxquestions`
但是,如果您是PHP的新手并尝试这样做,您可能会发现与$ this的使用有关的其他问题-您刚刚找到了第一个。 仅供参考,您的问题实际上将存在于任何版本的PHP中。降级到7.0与它没有任何关系。