注意试图获得非对象的属性

时间:2014-06-07 14:51:13

标签: php mysql function pdo

这有什么错误请帮忙!

这是来自mysql的查询:

public function stgradu($condition=">=50"){
    $stmt=$this->db->prepare("SELECT
    courses_has_schedule.mark as mark,
    Sum(courses.crs_hours) as total_hrs
    FROM
    courses_has_schedule
    Inner Join courses ON courses_has_schedule.crs_id = courses.crs_id
    Inner Join schedule ON schedule.sch_id = courses_has_schedule.sch_id
    Inner Join student ON student.st_id = schedule.st_id

    where
    student.st_id=? AND courses_has_schedule.mark $condition");
    $stmt->execute(array($this->getCurrentStudent(true)));
    return $stmt->fetchAll();
}

当我运行这个if语句时:

$stgr = $dataService->stgradu(">=50");
if(($stgr->total_hrs==7) AND ($stgradu->mark >= 50)) {
  echo Yes*";   
}else
  echo "No";

当我运行应用程序时,它给了我这个错误 (( Notice: Trying to get property of non-object in C:\xampp\htdocs\samer\stgr.php on line >12))

请大家帮忙!

3 个答案:

答案 0 :(得分:0)

fetchAll返回一个数组,你试图像对象一样访问它。试试这个:     if(($ stgr [' total_hrs'] == 7)AND($ stgradu [' mark']> = 50)){

答案 1 :(得分:-1)

您需要将PDO::FETCH_OBJ传递给fetchAll(),因为它默认返回一个关联数组。

public function stgradu($condition=">=50"){
    $stmt=$this->db->prepare("SELECT
    courses_has_schedule.mark as mark,
    Sum(courses.crs_hours) as total_hrs
    FROM
    courses_has_schedule
    Inner Join courses ON courses_has_schedule.crs_id = courses.crs_id
    Inner Join schedule ON schedule.sch_id = courses_has_schedule.sch_id
    Inner Join student ON student.st_id = schedule.st_id

    where
    student.st_id=? AND courses_has_schedule.mark $condition");
    $stmt->execute(array($this->getCurrentStudent(true)));
    return $stmt->fetchAll(PDO::FETCH_OBJ);
}

如果您应该只获得一行,那么将其更改为fetch(PDO::FETCH_OBJ),您的代码应运行正常。

答案 2 :(得分:-1)

thanx all ..我自己知道:P语法应该是......

  

返回$ stmt-> fetchAll()[0];