【问题标题】:Why can't I get Trying to get property of non-object error here?为什么我在这里不能得到 Trying to get property of non-object 错误?
【发布时间】:2019-04-06 23:29:17
【问题描述】:

我在 PHP 中创建了一个 MVC 格式,我收到了Trying to get property of non-object 错误,因为例程对象为空,而你看到它是假的。简而言之,这里发生的是我得到了一个具有routine_id 的$event 对象,我正在寻找与事件对象具有相同routine_id 的例程对象,但是我收到了这个错误:

none 注意:试图在 G:\5th 中获取非对象的属性 第 13 行学期\电子商务\xampp\htdocs\app\controllers\Event.php bool(false) aaaaaaaaaaaaaaaaarray(1) { [0]=> 对象(Event_model)#6 (8) { ["name"]=> NULL ["user_id"]=> NULL ["routine_id"]=> 字符串(2) "20" ["year"]=> string(4) "2019" ["month"]=> string(1) "3" ["day"]=> 字符串(2)“27”[“event_id”]=>字符串(1)“3” ["_connection":protected]=> 对象(PDO)#7 (0) { } } }

我尝试 var_dump $routine 和 $event 所以 $event 显示routine_id,但 $routine 为空。

测试代码:

var_dump($routine);
echo "aaaaaaaaaaaaaaaaaa";
var_dump($event);

public function index($y, $m, $d) {
    $event = $this->model('Event_model');
    $event = $event->getEventsbydate($y, $m, $d);

    $routine = $this->model('Routine_model'); 
    $routine = $routine->getRoutine($event->routine_id);

    if($event == false){ //if there is nothing in the workout_list

        header("location:/Event/create/$y/$m/$d"); //mylist controller , create method
    } else {
        var_dump($routine);
        echo "aaaaaaaaaaaaaaaaaa";
    var_dump($event);

        //header('location:/Routine/index_bycalendar/$event->routine_id'); 
    }
}

<?php
//for event
public function insert() {
$stmt = $this
    ->_connection
    ->prepare("INSERT INTO event(routine_id, year, month, day) VALUES(:routine_id, :year, :month, :day)");
$stmt->execute(['routine_id' => $this->routine_id, 'year' => $this->year, 'month' => $this->month, 'day' => $this->day]);
return $stmt->rowCount();
}

public function getEventsbydate($year, $month, $day) {
    $stmt = $this
        ->_connection
        ->prepare("SELECT * FROM event WHERE year = :year AND month = :month AND day = :day");
    $stmt->execute(['year' => $year, 'month' => $month, 'day' => $day]);
    $stmt->setFetchMode(PDO::FETCH_CLASS, "Event_model"); //datatype user
    return $stmt->fetchAll();
}

public function getRoutine($routine_id) {
    $stmt = $this
        ->_connection
        ->prepare("SELECT * FROM routine WHERE routine_id = :routine_id");
    $stmt->execute(['routine_id' => $routine_id]);
    $stmt->setFetchMode(PDO::FETCH_CLASS, "Routine_model"); //datatype user
    return $stmt->fetch(); //it should return a user
}

【问题讨论】:

  • 尝试使用硬编码routine_id在你的sql客户端(例如在phpMyAdmin中)中的方法getRoutine()执行sql语句值 (20)。喜欢SELECT * FROM routine WHERE routine_id = 20。结果是什么?
  • 感谢您的评论我得到了我想要得到的行,即routine_id = 20

标签: php pdo


【解决方案1】:

当你打电话时......

$event = $event->getEventsbydate($y, $m, $d);

这应该运行 SQL 然后返回...

return $stmt->fetchAll();

然后你使用$event in

$routine = $routine->getRoutine($event->routine_id);

BUT fetchAll() 返回一个事件数组,所以如果您只期待一个事件,那么您可以选择其中之一...

return $stmt->fetch();

使用fetch() 只返回一行,或者

$routine = $routine->getRoutine($event[0]->routine_id);

所以使用$event[0] 选择第一行。

可能你会有多个事件,在这种情况下你可能需要一个循环并创建多个Routine_models。

【讨论】:

    猜你喜欢
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 2021-09-23
    • 2020-07-18
    • 2021-01-06
    相关资源
    最近更新 更多