douao7937 2013-07-22 06:31
浏览 27
已采纳

类查询非对象的PDO属性

I not understand my error because I'm sure to have an oject.Look my print_r below:

Notice: Trying to get property of non-object in C:\xampp\htdocs\travel_mvc\cockpit\index.php on line 21

And line 21 and Have:

echo $row->tz_name.'<br>';

My print_r:

Array
(
    [0] => stdClass Object
        (
            [tz_name] => Africa/Abidjan
        )

    [1] => stdClass Object
        (
            [tz_name] => Africa/Accra
        )

    [2] => stdClass Object
        (
            [tz_name] => Africa/Addis_Ababa
        )

    [3] => stdClass Object
        (
            [tz_name] => Africa/Algiers
        )

    [4] => stdClass Object
        (
            [tz_name] => Africa/Asmara
        )

)

In my class I have :

class model{
    private $fields = '*';
    private $table = '';
    private $where = '';
    private $and = '';
    private $limit = '';
    private $order = '';
    private $type_order = 'DESC';
    private $d = array();

    public function find($data = array()){
        if(isset($data['fields'])){
            $this->fields = $data['fields'];
        }
        $this->table = $data['table'];
        if(isset($data['where'])){
            $this->where = ' WHERE '.$data['where'];
        }
        if(isset($data['and'])){
            $this->and = ' WHERE '.$data['and'];
        }
        if(isset($data['limit'])){
            $this->limit = ' LIMIT '.$data['limit'];
        }
        if(isset($data['order'])){
            $this->order = ' ORDER BY '.$data['order'].$type_order;
        }
        $query = Db::getInstance()->prepare('SELECT '.$this->fields.' 
                                             FROM '.$this->table.$this->where.
                                             $this->and.$this->order.$this->limit.'');
        $query->execute();
        while($data = $query->fetchAll(PDO::FETCH_OBJ)){
            $d[] = $data;
        }
        return($d);
    }
}

And when I call my function:

$model = new model();
$sql = $model->find(array(
    'fields'=>'tz_name',
    'table'=>'times_zones',
    'limit'=>5
));
foreach($sql as $row):
    echo $row->tz_name.'<br>';
endforeach;
echo '<pre>';
print_r($row);
echo '</pre>';
  • 写回答

1条回答 默认 最新

  • dsfw2154 2013-07-22 06:37
    关注

    PDOStatement::fetchAll() already returns an array so you're just double-nesting the result set for no reason.

    In your model::find() method, change these lines...

    while($data = $query->fetchAll(PDO::FETCH_OBJ)){
        $d[] = $data;
    }
    return($d);
    

    to

    return $query->fetchAll(PDO::FETCH_OBJ);
    

    You can also remove the model $d property (not that you were using it anyway).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动