Php 试图将数组从一个类传递到另一个类

Php 试图将数组从一个类传递到另一个类,php,oop,class,Php,Oop,Class,我是OOP新手,我正在努力掌握它。我要做的是返回表中的所有数据。现在只有2行,但我不确定如何将所有数据放在一个数组中,然后将其返回到$results变量 请帮忙 <?php include('dates.php'); $events = new dates(); echo $events->getNumberEvents(); <-----returns the number of rows in the table (Works as expected) $results

我是OOP新手,我正在努力掌握它。我要做的是返回表中的所有数据。现在只有2行,但我不确定如何将所有数据放在一个数组中,然后将其返回到$results变量

请帮忙

<?php
include('dates.php');

$events = new dates();
echo $events->getNumberEvents(); <-----returns the number of rows in the table (Works as expected)

$results = $events->getRows(); <------Doesn't work.

?>

如果你想在那个阶段做点什么的话。要简单返回,您可以执行以下操作:

return $this->db->queryString($sql);
要做:

foreach($results as $result)
{

    echo $result->fieldname;

}
要响应一切,请执行以下操作:

foreach($results as $result)
{
    foreach($result as $key => $value)
    {
        echo $key.': '.$value;
    }

}

作为对象而不是数组进行访问的原因是mysqli将行作为对象返回。

如果我只返回$this->db->queryString($sql),那么如何输出所有值?如果在原始调用中执行此操作:$results=$events->getRows();foreach($key=>$val的结果){echo$val;}它只打印出第一行数据的最后一列。但是,如果我不想在getRows函数中回显,而只想将结果返回到原始调用,该怎么办?fieldname应该指的是什么?要打印的db中的字段。如果在foreach循环中打印($result),它将显示整个对象,您将看到其中的字段。使用print\r可以看到数组包含所有数据,但我仍然不确定用什么替换fieldname。数组的前几个字段是[id]=>1[heading]=>Christmas Party等。。。。。
return $this->db->queryString($sql);
foreach($results as $result)
{

    echo $result->fieldname;

}
foreach($results as $result)
{
    foreach($result as $key => $value)
    {
        echo $key.': '.$value;
    }

}