Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP PDO sqlite3->;fetchAll()返回NULL_Php_Pdo_Sqlite - Fatal编程技术网

PHP PDO sqlite3->;fetchAll()返回NULL

PHP PDO sqlite3->;fetchAll()返回NULL,php,pdo,sqlite,Php,Pdo,Sqlite,我有下面的代码,我不明白为什么会返回NULL。我在浏览器中获得以下输出: string(5)“admin” array(3){[“is_admin”]=>NULL[“username”]=>NULL[“password”]=>NULL} ORM.php <?php class ORM { private $pdo; private $query; public function __construct() {

我有下面的代码,我不明白为什么会返回NULL。我在浏览器中获得以下输出:

string(5)“admin”

array(3){[“is_admin”]=>NULL[“username”]=>NULL[“password”]=>NULL}

ORM.php

<?php
    class ORM
    {
        private $pdo;
        private $query;

        public function __construct() {
            try {
                if ($this->pdo == NULL) {
                    $this->pdo = new \PDO('sqlite:' . SQLITE3_FILE);
                }
            } catch(\PDOException $e) {
                die($e);
            }

            return $this->pdo;
        }

        public function read($query) {
            try {
                $this->query = $this->pdo->query($query);

                $users = [];

                while ($record = $this->query->fetchAll(\PDO::FETCH_ASSOC)) {
                    $users = [
                        'is_admin' => $record['is_admin'],
                        'username' => $record['username'],
                        'password' => $record['password']
                    ];
                }

                return $users;
            } catch(\PDOException $e) {
                die($e);
            }
        }
    }

$record
在您的代码中是一个数组数组,因此为了在返回的数据集的每一行上进行迭代,您需要执行以下操作:

 public function read($query) {
        try {
            $this->query = $this->pdo->query($query);

            $users = [];
            $records = $this->query->fetchAll(\PDO::FETCH_ASSOC);


                foreach( $records as $record )
                {
                   //add users here using $record['is_admin']...                     
                }

            return $users;
        } catch(\PDOException $e) {
            die($e);
        }
    }

$record
在您的代码中是一个数组数组,因此为了在返回的数据集的每一行上进行迭代,您需要执行以下操作:

 public function read($query) {
        try {
            $this->query = $this->pdo->query($query);

            $users = [];
            $records = $this->query->fetchAll(\PDO::FETCH_ASSOC);


                foreach( $records as $record )
                {
                   //add users here using $record['is_admin']...                     
                }

            return $users;
        } catch(\PDOException $e) {
            die($e);
        }
    }

我学会了不按_构造返回,不知道这是否解决了您的问题。@Igor Unger:我仍然得到相同的结果我学会了不按_构造返回,不知道这是否解决了您的问题。@Igor Unger:我仍然得到了与您推荐的更改相同的结果,我从数据库返回最后一行,而不是受$_POST['username']的限制,方法是将SQL语句更改为如下所示的结果:“SELECT*from
users
WHERE username LIKE'$username',并进行您推荐的更改,我从数据库返回最后一行,而不是受$_POST['username']的限制,方法是将SQL语句更改为如下所示的结果:“SELECT*from
users
WHERE username LIKE'$username'”
 public function read($query) {
        try {
            $this->query = $this->pdo->query($query);

            $users = [];
            $records = $this->query->fetchAll(\PDO::FETCH_ASSOC);


                foreach( $records as $record )
                {
                   //add users here using $record['is_admin']...                     
                }

            return $users;
        } catch(\PDOException $e) {
            die($e);
        }
    }