Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP加载MySQL数据,但结果格式错误_Php_Mysql - Fatal编程技术网

使用PHP加载MySQL数据,但结果格式错误

使用PHP加载MySQL数据,但结果格式错误,php,mysql,Php,Mysql,在我的应用程序中,我想用PHP将所有ReservationType加载到中。Reservationtype有2个参数(reservationtypeid、ReserverUngart)。我使用中的数据库类来简化SQL命令 保留类型模型 class Reservierungstyp { public function __construct($reservationtypeid, $reservierungsart){ $this->$reservationtypei

在我的应用程序中,我想用PHP将所有ReservationType加载到
中。Reservationtype有2个参数(reservationtypeid、ReserverUngart)。我使用中的数据库类来简化SQL命令

保留类型模型

class Reservierungstyp
{
    public function __construct($reservationtypeid, $reservierungsart){
        $this->$reservationtypeid = $reservationtypeid;
        $this->$reservierungsart = $reservierungsart;
    }
}
在我的ReservationtypeModel中,我使用此方法getAllReservationtypes:

public function getAllReservierungstypen(){
    $db = new Database();
    $results = $db->query('SELECT * FROM reservationtype')->fetchAll();
    $rows = count($results);
    $db->close();

    $typesArray = array();

    if($rows > 0){
        foreach($results as $res){
            $r = new Reservierungstyp($res['reservationtypeid'], $res['reservierungsart']);
            $typesArray[] = $r;
        }
    }else{
        return null;
    }
    return $typesArray;
}
结果的格式如下所示:

Reservierungstyp Object ( [1] => 1 [Training] => Training ) )
但它不一定是这样的吗

Reservierungstyp Object ( [reservationtypeid] => 1 [reservierungart] => Training ) )

我不知道哪个错误会导致这个错误。如果我的代码中有另一个错误,请告诉我,我只是认为这个错误的格式是错误的触发器

我认为您的类应该是:

class Reservierungstyp
{
    private $reservationtypeid;
    private $reservierungsart;

    public function __construct($reservationtypeid, $reservierungsart){
        $this->reservationtypeid = $reservationtypeid;
        $this->reservierungsart = $reservierungsart;
    }
}

注意施工作业。您使用了$this->$reservationtypeid(class属性之前的美元),这意味着您需要一个变量,在本例中,它实际上是构造函数参数。

如果某个对象返回Object,则它不是JSON,那么问题出在哪里?我只是认为这是一个json问题,很抱歉注释错了。很难说清楚,因为您没有给我们提供
reserverungstyp
@FlashThunder的类定义,我现在就添加这个,好吧,它与json没有任何关系,那么您还需要提供数据库表模式