【发布时间】: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 秒”错误