【问题标题】:Having issues with understanding how to fetch data on a while loop [duplicate]在理解如何在while循环中获取数据时遇到问题[重复]
【发布时间】:2019-07-28 09:45:40
【问题描述】:

我正在尝试使用 while 循环来获取和显示用户数据。我已经创建了 get 类和 result 类来处理这个问题,但我认为我可能会缺少一些东西,因为我是 OOP 的新手

我已经尝试使用结果对象,但我得到了 **Notice: Undefined index: type in the ** on the table

这是我的课

    private static $_instance = null;
    private $_pdo, 
            $_query, 
            $_error = false, 
            $_results,
            $_count = 0;

    private function __construct(){
        try{
            $this->_pdo = new PDO('mysql:host='. config::get('mysql/host') .';dbname='. config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
        }catch(PDOException $e){
            die($e->getMessage());
        }
    }
    public static function getInstance(){
    if(!isset(self::$_instance)){
        self::$_instance = new DB;
    }
    return self::$_instance;
    }

    public function query($sql, $params = array()){
        $this->_error = false;
        if($this->_query = $this->_pdo->prepare($sql)){
            $x = 1;
            if(count($params)){
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }

            if($this->_query->execute()){
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
            }else{
                $this->_error = true;
            }
        }
        return $this;
    }
    public function action($action, $table, $where = array()){
        if(count($where) === 3){
            $operators = array('=', '>', '<', '>=', '<=');

            $field   = $where[0];
            $operator   = $where[1];
            $value  = $where[2];

            if(in_array($operator, $operators)){
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";

                if(!$this->query($sql, array($value))->error()){
                    return $this;
                }
            }

        }
        return false;
    }
    public function get($table, $where){
        return $this->action("SELECT *", $table, $where);
    }
    public function results(){
        return $this->_results;
    }

这是我的while循环

<?php
                                               $transactions = DB::getInstance()->get('transactions', array('user_id', '=', $user->data()->user_id));
                                               if($transactions->count() < 1){
                                                   ?>
                                                   <td colspan='5'>No transaction yet on your account</td>
                                                   <?php
                                               }else{ 
                                                    while($trans = $transactions->results()){
                                               ?>
                                                   <th scope="row"><?php echo $trans->date?></th>
                                                   <td><?php echo $trans->type?></td>
                                                   <td><span class="label label-info"><?php echo $trans->description?></span></td>
                                                   <td><?php echo $trans->description?>/td>
                                                   <td>
                                                   <span class="label label-primary"><?php
                                                   if($trans->status === 0){
                                                       echo 'PENDING';
                                                   }else if($trans->status === 1){
                                                       echo 'PROCESSING';
                                                   }else{
                                                       echo 'CONFIRMED';
                                                   }
                                                   ?></span>
                                                   </td>
                                                   <?php
                                                   }
                                               }
                                               ?>

我希望显示单个行,但我收到 Notice: Undefined index: 错误

【问题讨论】:

  • 实际上这是显示的错误:- 注意:尝试获取非对象的属性“日期”
  • 请帮忙,这不是很有帮助
  • 我还收到“致命错误:第 120 行的 C:\xampp\htdocs\oop\classes\DB.php 中的最大执行时间超过 30 秒”错误

标签: php oop pdo


【解决方案1】:

在你的数据库类中你有

    public function results(){
        return $this->_results;
    }

和

...
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
...

这意味着$transactions-&gt;results() 正在从数据库中返回一个结果数组,而不是您所期望的单行。

这解释了第一个错误,因为您尝试在数组上运行 $trans-&gt;date。

此外,由于您从不修改结果数组 $transactions-&gt;results() 将继续一遍又一遍地返回相同的数组,直到超时。

我没有查看所有代码以查看是否还有其他问题,但要解决您的直接问题,您只需使用 foreach 循环而不是 while

foreach ($transactions->results() as $trans) {
   ...

}

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多