【问题标题】:Warning: Invalid argument supplied for foreach() 10警告:为 foreach() 10 提供的参数无效
【发布时间】:2018-11-29 11:59:01
【问题描述】:

代码需要返回"alex",但它返回:

“警告:为 foreach() 提供的参数无效”

我找不到解决方案,所以我寻求帮助。

index.php:

$user = DB::getInstance()->get('users', array('username', '=', 'alex'));

if (!$user->count()) {
    echo 'No user';
} else {

    foreach ($user->results() as $user ) {
        echo $user->username, '<>';
    }
}

【问题讨论】:

  • var_dump($user-&gt;results())
  • 尝试在 foreach 中直接使用$user 而不是$user-&gt;results()
  • var_dump $user->results()s 在 if 语句之前,看看你是否得到任何你可能没有得到结果的东西(如果你正在跟随你应该使用的代码课程 OOP 登录课程$user->first() 取回 1 个结果)
  • 也许将 $user->results() 分配给一个变量,然后在 foreach 中使用它
  • 它返回相同的警告。

标签: php foreach warnings


【解决方案1】:

我发现了错误。它在 db.php 的第 43 行出现错字

I wrote : $this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ);

必须是:

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

我在结果中错过了“s”。 感谢您的帮助。

【讨论】:

    【解决方案2】:

    这是一个 db.php

     class DB {
         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->_result = $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 delete ($table, $where) {
            return $this->action('DELETE ' , $table, $where);
    
    
        }
    
        public function results() {
            return $this->_results;
    
        }
    
    
        public function error() {
    
            return $this->_error;
    
        }
        public function count() {
            return $this->_count;
    
        }
     }
    

    【讨论】:

      【解决方案3】:

      试试,

          $user = DB::getInstance()->get('users', array('username', '=', 'alex'));
      
      if(!$user->count()) {
      echo 'No user';
      } else {
          if(!empty($user->results())){
              foreach($user->results() as $user ) {
                  echo $user->username, '<>';
      
              }
         }
      
      }
      

      【讨论】:

      • 这也不对。您根据一件事的条件返回“无用户”消息,但随后可能不返回任何消息或基于另一件事的输出。 if (!$user-&gt;count() || empty($user-&gt;results()) ) { echo "no user"; } else {
      • @Anjali Patil - 什么都不返回(空白页)
      • @James - 返回“无用户”。
      • 请提供 $user->count() 和 $user->results() 的输出,以便我们清楚地了解响应。
      • @AnjaliPatil $user->count() 返回“1”(一个)
      猜你喜欢
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多